none
length of a string without using any built-in functions RRS feed

  • Question

  • code that returns the length of a string without using any built-in functions.

    thanks

    Sam

    Thursday, December 23, 2010 12:23 PM

Answers

  • Hi,

    I'm not sure if it's too late to saying this that I have done this before in my blog ;)

    Please see: http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!376.entry?&_c02_vws=1 (Sample Code: Boyer-Moore pattern matching)

    Best regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    • Marked as answer by Mio_Miao Friday, December 31, 2010 8:47 AM
    Thursday, December 23, 2010 3:20 PM
  • Here is my code without using inbuilt functions

    That's debatable: String.Length and its indexer can be considered "builtin or library functions". Your code is actually calling the get_Length and get_Chars methods. As I said in another thread, there's not much you can do with a string without calling a method.


    Well...

    I've optimized the loop a bit, and got rid of the internal get_chars method...

    Anyone got an idea on how to get rid of the get_Length method?

     

     private int Test()
        {
          string myString = "This string may contain many string inside this string";
          string testString = "string";
    
          char[] myChars = new StringToChar { str = myString }.chr;
          char[] testChar = new StringToChar { str = testString }.chr;
    
          int countResult = 0;
    
          int myCharsLength = myChars.Length; //DAMMIT
          int testCharLength = testChar.Length; //DAMMIT
    
          for (int i = 0; i < myCharsLength - testCharLength + 1; i++)
          {
            if (myChars[i] == testChar[0])
            {
              for (int j = 1; j < testCharLength; j++)
              {
                if (myChars[i + j] != testChar[j])
                {
                  goto endOfCharAnalyses;
                }
              }
    
              countResult++;
            }
    
          endOfCharAnalyses:
            continue;
          }
    
          return countResult;
        }
    
    
    
        [StructLayout(LayoutKind.Explicit)]
        public struct StringToChar
        {
          [FieldOffset(0)]
          public string str;
          [FieldOffset(0)]
          public char[] chr;
        }
    

    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    • Proposed as answer by Jan Van der Haegen Thursday, December 23, 2010 12:55 PM
    • Proposed as answer by Matthew Watson Thursday, December 23, 2010 1:58 PM
    • Marked as answer by Mio_Miao Friday, December 31, 2010 8:12 AM
    Thursday, December 23, 2010 11:09 AM

All replies

  • Hi,

    I need to find whether a string s1 is available in another string s2 without using s2.contains  or any other library functions.Also I need to find the number of occurences of s1 in s2.

    Please let me know a simple solution for this..

    thanks

    Sam

     

    • Merged by Rudedog2 Thursday, December 23, 2010 1:20 PM : Duplicate Post
    Thursday, December 23, 2010 5:18 AM
  • Here is my code without using inbuilt functions to check if a sub string exists, if yes its index and how many times..

     

                string string1 = "This string may contain many string inside this string";
                string string2 = "string";
                int foundCount = 0;
                int occurenceCount = 0;

                for (int indexStr2 = 0; indexStr2 < string2.Length; indexStr2++)
                {
                    for (int indexStr1 = 0; indexStr1 < string1.Length; indexStr1++)
                    {
                        foundCount = 0;

                        if (string2[indexStr2] == string1[indexStr1]) //If we find "s" somewhere...
                        {
                            for (int index = 0; index < string2.Length; index++ )
                            {
                                if (string1.Length > (indexStr1 + index) && string2.Length > (indexStr2 + index))
                                {
                                    if (string2[indexStr2 + index] == string1[indexStr1 + index])
                                    {
                                        foundCount++;
                                    }
                                }
                            }


                            if (foundCount == string2.Length)
                            {
                                Console.WriteLine("The string was found at index : " + indexStr1.ToString());
                                occurenceCount++;
                            }
                        }
                    }

                }
                Console.WriteLine("The string was found " + occurenceCount.ToString()+  " times!");

     


    Happy to Help :)
    Thursday, December 23, 2010 5:43 AM
  • Here is my code without using inbuilt functions

    That's debatable: String.Length and its indexer can be considered "builtin or library functions". Your code is actually calling the get_Length and get_Chars methods. As I said in another thread, there's not much you can do with a string without calling a method.
    Thursday, December 23, 2010 8:13 AM
  • hmm I missed that! Thank you Louis.fr for clearing it out.. :)

    sams6 is it good enough for you?


    Happy to Help :)
    Thursday, December 23, 2010 8:18 AM
  • Can you give me the link to that thread.

    thanks

    Sam

    Thursday, December 23, 2010 10:04 AM
  • lol Louis... good point. Is it even possible to do such a code (or any one) with not-buit-it functions?
    Thursday, December 23, 2010 10:30 AM
  • Here is my code without using inbuilt functions

    That's debatable: String.Length and its indexer can be considered "builtin or library functions". Your code is actually calling the get_Length and get_Chars methods. As I said in another thread, there's not much you can do with a string without calling a method.


    Well...

    I've optimized the loop a bit, and got rid of the internal get_chars method...

    Anyone got an idea on how to get rid of the get_Length method?

     

     private int Test()
        {
          string myString = "This string may contain many string inside this string";
          string testString = "string";
    
          char[] myChars = new StringToChar { str = myString }.chr;
          char[] testChar = new StringToChar { str = testString }.chr;
    
          int countResult = 0;
    
          int myCharsLength = myChars.Length; //DAMMIT
          int testCharLength = testChar.Length; //DAMMIT
    
          for (int i = 0; i < myCharsLength - testCharLength + 1; i++)
          {
            if (myChars[i] == testChar[0])
            {
              for (int j = 1; j < testCharLength; j++)
              {
                if (myChars[i + j] != testChar[j])
                {
                  goto endOfCharAnalyses;
                }
              }
    
              countResult++;
            }
    
          endOfCharAnalyses:
            continue;
          }
    
          return countResult;
        }
    
    
    
        [StructLayout(LayoutKind.Explicit)]
        public struct StringToChar
        {
          [FieldOffset(0)]
          public string str;
          [FieldOffset(0)]
          public char[] chr;
        }
    

    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    • Proposed as answer by Jan Van der Haegen Thursday, December 23, 2010 12:55 PM
    • Proposed as answer by Matthew Watson Thursday, December 23, 2010 1:58 PM
    • Marked as answer by Mio_Miao Friday, December 31, 2010 8:12 AM
    Thursday, December 23, 2010 11:09 AM
  • By the way, I did not consider my StringToChar._ctor() to be a "builtin or library function" :p
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 11:11 AM
  • How about

     

                int length = 0;

                foreach(Char ch in string1)
                {
                    if (ch != '\0')
                    {
                        length++;
                    }

                }

     

    I hope Char ch in string1 is not using something internally! :D


    Happy to Help :)
    Thursday, December 23, 2010 11:21 AM
  • public sealed class String : IEnumerable<char>
    
      public interface IEnumerable<out T> : IEnumerable
      {
        // Summary:
        //   Returns an enumerator that iterates through the collection.
        //
        // Returns:
        //   A System.Collections.Generic.IEnumerator<T> that can be used to iterate through
        //   the collection.
        IEnumerator<T> GetEnumerator();
      }
    
    the foreach uses the (Ienumerable).GetEnumerator() ... :'(
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 11:31 AM
  • How about

     

                int length = 0;

                foreach(Char ch in string1)
                {
                    if (ch != '\0')
                    {
                        length++;
                    }

                }

     

    I hope Char ch in string1 is not using something internally! :D


    Happy to Help :)

    Yes of course it is. :)

     

    foreach will call String.GetEnumerator()

    Thursday, December 23, 2010 11:50 AM
  • Go do your homework yourself.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com
    Thursday, December 23, 2010 12:31 PM
  • Where`s the point?

    Are you in ice age?

     

    btw, INDEED,  I agree with Geert, why are you asking these stupid questions here.... use google if nothing else...

    Thursday, December 23, 2010 12:32 PM
  • Many ways this can be done, here is a simple example.

     

        private void StrLength()
        {
          int count = 0;
          string str = "hello";
          foreach (char ch in str)
          {
            count++;
          }
    
          MessageBox.Show("Length: " + count.ToString());
        }
    Thursday, December 23, 2010 12:33 PM
  • This question is a small part to help us find a reply to this post... http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b33ab190-d585-4ad0-b521-5348b0ad1e38  it's the part of the puzzle we have yet to solve.
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 12:43 PM
  • And Steven_Shultz, the foreach uses the built in function GetEnumerator()...
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 12:44 PM
  •   [StructLayout(LayoutKind.Explicit)]
      public struct StringToChar
      {
       [FieldOffset(0)]
       public string str;
       [FieldOffset(0)]
       public char[] chr;
      }
    

     

    I didn't thought it was possible to use this. The default constructor of a struct is not a method call. The Length property of an array isn't either.

    I'm going to inspect the documentation to find why it works when you use array instructions on a string. But it actually does the job without any method being called.

    • Proposed as answer by Rudedog2 Thursday, December 23, 2010 2:03 PM
    • Unproposed as answer by Rudedog2 Thursday, December 23, 2010 2:08 PM
    Thursday, December 23, 2010 12:53 PM
  •  

    I didn't thought it was possible to use this. The default constructor of a struct is not a method call. The Length property of an array isn't either.

    I'm going to inspect the documentation to find why it works when you use array instructions on a string. But it actually does the job without any method being called.


    Well, if the default ctor of a struct, and the length of an array aren't method calls, then I believe we sorted it out? I have proposed my first reply as answer based on your believes. Just a note: remove "//DAMMIT" when u hand this in as a school assignment :-p...

    The use of the struct was to avoid the ToCharArray() method call btw.... :-)


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 12:57 PM
  • I agree to RootKid. Using the class String directly leads us to use build in functions. One key concept of object oriented languages is encapsulation. You do not have access to the core representation of the data. The only access is through the public methods.

    So I would argument, that there is no valid solution to this question. (And how did someone got to this? Did someone just translated a C / C++ thing to c# and is wondering now, that we cannot move a char pointer till we get the zero in it which is the delimiter of the string (Either as direct pointer or through an array of course!)?)

    With kind regards,

    Konrad

    Thursday, December 23, 2010 1:05 PM
  • Actually, it's our own fault that has lead to this question... :(

     http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b33ab190-d585-4ad0-b521-5348b0ad1e38 


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 1:07 PM
  • Hmm. I somehow doubt, that this StringToChar is that legal in this context. If nothing is called in String, then it is something, where you run into trouble if the Class String is changed. There must be something that giving you the char[] array inside String.

    But once you get the char[] myChars: The length is then myChars.Length.

    My perspective is, that the data of the string is private inside and you have to access official functions to get it. So maybe it could be worth a try to check reflection and read the core data. That way, we are not using any functions of a String.

    Just my 2 cent

    Konrad

    • Proposed as answer by Matthew Watson Thursday, December 23, 2010 1:31 PM
    Thursday, December 23, 2010 1:15 PM
  • Not sure if we can use .Length either.

    Thursday, December 23, 2010 1:20 PM
  • I do not see anything wrong with asking questions about homework.  It is one reason the forum is here.  It is a venue to ask programming related questions. 

    It provides an opportunity for all to learn something.  It allows answerers to reflect on old problems in a new light, while reinforcing many concepts that you learned so long ago that you take them for granted.  Case in point.  Did you see what Louis posted?!?!  Whoa!  Most teachers will tell you that they feel that they learn more from their students than what they teach their students.

    It exposes the student asking the question to the process of peer review of their code.  Hopefully, the student will not simply be provided with a solution, which allows these teachable moments to be wasted.  Teaching is something I do in my spare time, and I find it very rewarding to see those rare moments of true enlightenment on the faces of students.

    Happy Coding to all of you.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 1:38 PM
  • This is a typical badly-worded homework question. The people writing the questions (I mean the tutors) need to be more precise. They need to specify exactly what "without using any built-in functions" means.

    Because, strictly speaking, you cannot access any data within a string, or any data about a string, without using built-in functions UNLESS you write unsafe code that uses pointers - which I'm pretty sure wasn't intended!

    So strictly speaking, the answer to the question is: "You can't".

    Thursday, December 23, 2010 1:42 PM
  • "There must be something that giving you the char[] array inside String."

    I have to disagree.

    A string is internally represented by a character array.  An array of characters will reside on the stack, not the heap.  Yes, we always learned that String is a reference type, but what goes on in your memory might still surprise you...

    A struct is internally represented by only it's private properties, sequentially on the stack.

    So basically, what I thought is happening by messing with the structlayout: is only tricking our programming a bit into thinking that the top X bytes on the stack represent a chararray, while actually we put them there as a string.

    The only purpose is to avoid the String.ToCharArray() method,.

     

    "run into trouble if the Class String is changed"

    I have to agree on this one...

     

    "That way, we are not using any functions of a String"

    Agreed, but the original question involved not using ANY builtin methods, not just "string", and defenitely not using reflection...

     


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 1:44 PM
  • The problem with this kind of homework questions is simple:

    People should try first. Learn a man how to fish, instead of giving him one. He didn't even try, he just copy/pasted the question to this forum.

    If you say: I need to know this for my homework, I tried this and that, you will ALWAYS get a good response from me. But not to questions like these. That's what's wrong with homework questions like this.


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Thursday, December 23, 2010 1:49 PM
  • You left out the second half of that quote, which is the most important part.

    "Give a man a fish, and you feed him for a day.  TEACH a man to fish, and you feed him for life."

     

    As for how much was in the initial post.  I always grant benefit of the doubt, that language is an obstacle.  And, you can always ask them to post their code so that someone can take a look at it.

    As I noted, the student gets the experience of peer review.  They learn how to ask questions, and present their problems.  Just don't forget that their are many people who just don't know enough to describe the problem that they have.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 1:57 PM
  • Fact is that this thread is hogging up a lot of experts and has some very interesting replies.
    Thursday, December 23, 2010 2:02 PM
  • That code with the struct fails in release build.

    Try a RELEASE build of this; it prints "C" and not "A" as you might have hoped...

    using System;
    using System.Runtime.InteropServices;
    
    namespace Demo
    {
      class Program
      {
        static void Main(string[] args)
        {
          string test = "ABCDEF";
          char[] chars = new StringToChar{str=test}.chr;
          Console.WriteLine("First letter = " + chars[0]);
        }
      }
    
      [StructLayout(LayoutKind.Explicit)]
      public struct StringToChar
      {
        [FieldOffset(0)]
        public string str;
        [FieldOffset(0)]
        public char[] chr;
      }
    }
    

    Thursday, December 23, 2010 2:03 PM
  •  [StructLayout(LayoutKind.Explicit)]
     public
     struct
     StringToChar
     {
      [FieldOffset(0)]
      public
     string
     str;
      [FieldOffset(0)]
      public
     char
    [] chr;
     }
    
    Stop doing stuff like that.  That's unnatural.  You fooled it.  I like it.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 2:05 PM
  •  An array of characters will reside on the stack, not the heap.

    That's not true. If you declare an array of characters, it goes on the heap unless you use stackalloc.

    The struct hack is winding up with the character array pointing to the string that was declared on the heap. This is messed up in many ways, one of which is shown below.

    By changing elements of the char array, we are changing elements of the original, supposedly immutable, string! Also we get different results in debug and release builds as noted above, for extra messedupness. :)

    using System;
    using System.Runtime.InteropServices;
    
    namespace Demo
    {
      class Program
      {
        static void Main(string[] args)
        {
          string test = "ABCDEF"; // This is on the heap.
          char[] chars = new StringToChar{str=test}.chr;
          chars[0] = 'X';
          
          // In release, this prints "ABXDEF".
          // In debug, this prints "XBCDEF".
          // Either way, it's messed up. We've changed a supposedly immutable string!
    
          Console.WriteLine(test); 
        }
      }
    
      [StructLayout(LayoutKind.Explicit)]
      public struct StringToChar
      {
        [FieldOffset(0)]
        public string str;
        [FieldOffset(0)]
        public char[] chr;
      }
    }
    

    Thursday, December 23, 2010 2:10 PM
  • That's weird...

    Upon your post I immediately copied your code, ran the example from visual studio using Debug AND using Release, both gave correct results.  Even running the compiled .exe gives me the correct results... :-s Did you test it in VS 2010 or another version? :-s


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:11 PM

  • "The improbable we do, the impossible just takes a little longer." (Steven Parker)

    Welcome back.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 2:15 PM
  • Yes, testing with Visual Studio 2010, running on Windows 7 x64.

    I just tried it with x32, and it's ok there - so it only fails on x64 builds.

    However, on either x32 or x64 it allows us to change the contents of an immutable string WITHOUT using any unsafe code constructs! That's slightly scary. :)

    Thursday, December 23, 2010 2:16 PM

  • "The improbable we do, the impossible just takes a little longer." (Steven Parker)

    Welcome back.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx


    You recognized me by my signature? :-D

    Thanks!


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:19 PM
  • Yes, testing with Visual Studio 2010, running on Windows 7 x64.

    Same... In a little test project, compiled as winforms application. Let me do a console app to see the difference...
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:20 PM
  • You recognized me by my signature? :-D

    Thanks!


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    That...and your BIG teeth.             =8^D

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 2:23 PM
  • Ran it with VS2008 (XP x86) and I'm getting some weird stuff.
    http://img.photobucket.com/albums/v647/sw_wever/weirdstuff.png
    Thursday, December 23, 2010 2:24 PM
  • Then use a For loop, or while

     

    Thursday, December 23, 2010 2:24 PM
  • Yes, testing with Visual Studio 2010, running on Windows 7 x64.

    Same... In a little test project, compiled as winforms application. Let me do a console app to see the difference...
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Sorry I forgot to say that I was testing in a console app!
    Thursday, December 23, 2010 2:31 PM
  • What build CPU targets are you guys using?
    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 2:33 PM
  • You recognized me by my signature? :-D

    Thanks!


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    That...and your BIG teeth.             =8^D

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Lol :-p

    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:33 PM
  • However, on either x32 or x64 it allows us to change the contents of an immutable string WITHOUT using any unsafe code constructs! That's slightly scary. :)


    Not only scary, but in violation of the msdn reference that states: "Strings are immutable--the contents of a string object cannot be changed after the object is created"

    SEE THIS IS WHY WE SHOULDN'T DO KID'S HOMEWORKS FOR THEM!!! :-D


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:35 PM
  • Even more awesome: You can make the string a const, and it still changes the contents of it. :)

    How to change the contents of a const string without using unsafe code:

    using System;
    using System.Runtime.InteropServices;
    
    namespace Demo
    {
      class Program
      {
        static void Main(string[] args)
        {
          const string test = "ABCDEF";
          char[] chars = new StringToChar{str=test}.chr;
          chars[0] = 'X';
    
          // On an x32 release or debug build or on an x64 debug build, 
          // the following prints "XBCDEF".
          // On an x64 release build, it prints "ABXDEF".
          // In both cases, we have changed the contents of 'test' without using
          // any 'unsafe' code...
    
          Console.WriteLine(test);
        }
      }
    
      [StructLayout(LayoutKind.Explicit)]
      public struct StringToChar
      {
        [FieldOffset(0)]
        public string str;
        [FieldOffset(0)]
        public char[] chr;
      }
    }
    

    Thursday, December 23, 2010 2:40 PM
  • Hmm. I am a little confused by your tries.

    You always have to be very carefull what you do. Your approach (as I understood it) is very dangerous, because you get direct access to the data of the class without using the implemented interface.

    The implementation of a class might change! That is the reason for all this capsulation in OO development things. I didn't expect that but maybe there is any difference between 32 and 64 bit or whatever. So great: You build a solution and sell it and then you find out, that some customers cannot use it. Good luck finding such an error :)

    With kind regards,

    Konrad

    Thursday, December 23, 2010 2:48 PM
  • A string is internally represented by a character array.  An array of characters will reside on the stack, not the heap.  Yes, we always learned that String is a reference type, but what goes on in your memory might still surprise you...

    A struct is internally represented by only it's private properties, sequentially on the stack.

    So basically, what I thought is happening by messing with the structlayout: is only tricking our programming a bit into thinking that the top X bytes on the stack represent a chararray, while actually we put them there as a string.


    Wrong. True. And wrong.

    A string is internally represented by, in that order, an array length, a string length, the chars. None of them resides on the stack.

    An array is internally represented by, in that order, an array length and the chars. None of them resides on the stack.

    When you use the FieldOffset attribute to handle the string as a char array, you don't get anything right:

    - the Length returned is the "array length" of the string, which is equal to the string length + 1.

    - the chars returned by the array indexer are shifted by 2 chars (the length of the "string length" field).

    You can use the FieldOffset to make that work, but it needs a little bit more work.

      unsafe static int Test()
      {
        string myString = "This string may contain many string inside this string";
        string testString = "string";
    
        int countResult = 0;
    
        fixed (char* myChars = new StringToChar { str = myString }.chr, testChar = new StringToChar { str = testString }.chr)
        {
          // The 2 first chars of the array are actually the string length.
          int myCharsLength = myChars[1] << 16 | myChars[0];
          int testCharLength = testChar[1] << 16 | testChar[0];
    
          for (int i = 0; i < myCharsLength - testCharLength + 1; i++)
          {
            if (myChars[i + 2] == testChar[2])
            {
              for (int j = 1; j < testCharLength; j++)
              {
                var c = testChar[7];
                if (myChars[i + 2 + j] != testChar[j + 2])
                {
                  goto endOfCharAnalyses;
                }
              }
    
              countResult++;
            }
    
          endOfCharAnalyses:
            continue;
          }
        }
    
        return countResult;
      }
    
    Thursday, December 23, 2010 2:52 PM
  • Dear Konrad,

    thanks for your kind words of care, and I think I and everyone else agrees with you that we are past the point of sanity.

    Also, do not mistake us for conjurers of cheap tricks, we are the keepers of the key to the gate of the future, changing constant immutable strings as we please, ignoring the line between safe and unsafe, and in the end, the answer is eventually 42.


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 2:58 PM
  • I just tried it with x32, and it's ok there - so it only fails on x64 builds.

    I tried it on x86 in Debug mode and get the same problem, which is easily explained by the presence of an extra field in the String class.
    Thursday, December 23, 2010 2:59 PM
  • Not only scary, but in violation of the msdn reference that states: "Strings are immutable--the contents of a string object cannot be changed after the object is created"

    I guess that is only intended to mean that no method of the String class changes it. The String class has nothing special protecting it against changes.

    Thursday, December 23, 2010 3:04 PM
  • Reading back, I do not remember what I must have smoked at lunch today to write the stack<>heap thing.  (Or the previous post for that matter...).

     

    Louis.FR, I think your answer is the closest we got to a result for the original poster's question, thank you for your insights.

     

    Also, this has got to be one of the most interesting posts I've participated in, ever... :-D


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 3:04 PM
  • Use a for loop or a while, and stop when your index reaches the length of the string?

    Thursday, December 23, 2010 3:06 PM
  • Fact is that this thread is hogging up a lot of experts and has some very interesting replies.

    So.  The ship is under control.  Jump in.  The water's fine.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 3:08 PM
  • Fact is that this thread is hogging up a lot of experts and has some very interesting replies.

    So.  The ship is under control.  Jump in.  The water's fine.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    how are you doing Rud? 

    Working hard, I mean keeping the forum going as usually...

     

     

     


    Just Be Humble Malange!
    • Proposed as answer by Matthew Watson Wednesday, January 5, 2011 10:29 AM
    Thursday, December 23, 2010 3:16 PM
  • Hi,

    I'm not sure if it's too late to saying this that I have done this before in my blog ;)

    Please see: http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!376.entry?&_c02_vws=1 (Sample Code: Boyer-Moore pattern matching)

    Best regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    • Marked as answer by Mio_Miao Friday, December 31, 2010 8:47 AM
    Thursday, December 23, 2010 3:20 PM
  • Hi,

    I'm not sure if it's too late to saying this that I have done this before in my blog ;)

    Please see: http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!376.entry?&_c02_vws=1 (Sample Code: Boyer-Moore pattern matching)

    Best regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords

    Well Done Mate, I loved it...

     


    Just Be Humble Malange!
    Thursday, December 23, 2010 3:22 PM
  • Fact is that this thread is hogging up a lot of experts and has some very interesting replies.

    So.  The ship is under control.  Jump in.  The water's fine.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx


    been folowing this topic from the start.
    It has some great stuff posted in it.

    still, I got this error with VS2008 on XP 32 bits:
    http://img.photobucket.com/albums/v647/sw_wever/weirdstuff.png

    The release is even stranger :P

    Thursday, December 23, 2010 3:26 PM
  • Well Done Mate, I loved it...
    Just Be Humble Malange!

    Thank you Malange, nice that you enjoyed.
    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, December 23, 2010 3:31 PM
  • Hi,

    I'm not sure if it's too late to saying this that I have done this before in my blog ;)

    Please see: http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!376.entry?&_c02_vws=1 (Sample Code: Boyer-Moore pattern matching)

    Best regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords


    Pffftt... Too little, too late.

    Nah just kidding, found it an interesting read.  Am I right in my assumption that this has a pretty decent performance?


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Thursday, December 23, 2010 3:35 PM
  • Am I right in my assumption that this has a pretty decent performance?
    "The improbable we do, the impossible just takes a little longer." (Steven Parker)

    That implementaion's performance is great!
    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, December 23, 2010 3:40 PM
  • Hi,

    I'm not sure if it's too late to saying this that I have done this before in my blog ;)

    Please see: http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!376.entry?&_c02_vws=1  (Sample Code: Boyer-Moore pattern matching )

    Best regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords

    Wazzup!

    You need to take your blog and move it to a new web site.  That entire site support for blogs will soon be coming to an end really soon.  Like by the end of the year as I recall.  Try BlogSpot.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, December 23, 2010 3:43 PM
  • Wazzup!

    You need to take your blog and move it to a new web site.  That entire site support for blogs will soon be coming to an end really soon.  Like by the end of the year as I recall.  Try BlogSpot.

    Hi Rudy, how are you?

    Yes, you're right. I tried to move that but no success. I always get "Internet Explorer could not display the page...". Finally, I saved it to my computer to paste in another blog service.

    Thank you for the alert, Rudy.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, December 23, 2010 3:55 PM
  • Hi sams6,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?  
     
    If you have got some good solutions to this problem? please mark the useful reply as answer. If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Thank you for your understanding and support.

    I look forward to hearing from you.

    Best Regards,

    Mio

     


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 29, 2010 7:49 AM
  • Technically?  No you can't.  Properties are syntactic sugar introduced by the language.  They actually create a getter and setter method which is what you are actually accessing when you use a property.  For example, if I create property SomeProp, it actually creates two methods get_SomeProp() and set_SomeProp(value).
    Tuesday, March 15, 2011 8:31 PM
  • code that returns the length of a string without using any built-in functions.

    This is simply not possible. A String is a built-in class and there is no way to access it without using the methods and properties that the class provides.

    You could do something like access each successive character until you get an exception, and then record which position the exception occurred at. But you are still accessing the built-in methods and properties of the class.

    Perhaps you mean something else. You haven't been entirely clear about what you are trying to accomplish.


    Jonathan Wood • Black Belt Coder
    Sunday, March 20, 2011 5:32 AM

  • int Length = 0; 
    int j = 0; 
    char RevString ='\0'; 
    char tempString = '\0'; 
    char[] temp = new char[]{'a','b','c','d','e','f','\0'}; 
    //String temp= "Welcome to cluster"; 
    while('\0' != temp[index]) 
    index++; 
    Length = index; 
    System.out.println("String Length = " + Length);
    Wednesday, April 29, 2015 7:34 AM
  • its in Java
    Wednesday, April 29, 2015 7:35 AM