program to find the length of the string manually

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goelvivek
    New Member
    • Dec 2006
    • 26

    program to find the length of the string manually

    write a program to find the length of the string without using control structures and without using string.h header files???
  • PuzzleC
    New Member
    • Nov 2007
    • 9

    #2
    write likeness a standart strlen function.

    Sorry, it is strig.... string str; str.length(); ????

    Comment

    • goelvivek
      New Member
      • Dec 2006
      • 26

      #3
      but that will use a control structure like
      while(s!='\0')
      l++;

      and we don't have to use that

      can is their is method so that we print string and then calculate the pointer position at that time

      Comment

      • PuzzleC
        New Member
        • Nov 2007
        • 9

        #4
        see above... or convert to char* sl.c_str() and working as a character type

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by goelvivek
          write a program to find the length of the string without using control structures and without using string.h header files???
          A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.

          Comment

          • goelvivek
            New Member
            • Dec 2006
            • 26

            #6
            Originally posted by weaknessforcats
            A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
            is their is no answer

            Comment

            • PuzzleC
              New Member
              • Nov 2007
              • 9

              #7
              Originally posted by weaknessforcats
              A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
              what is a type C string?
              C++ string
              approximate:
              Code:
              cpp
              class string
               {
              private:                  
               struct ChatT
                {
                ...
                ...
                blablabla;
                };
               ChatT *s;
               int __length;  //<---- private mamber of class string
              public:
               int length() { return this->__length; }
               } ;

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                when you say the length of the string, does string refer to a variable of string type or is this a character array?

                Comment

                • chazzy69
                  New Member
                  • Sep 2007
                  • 196

                  #9
                  If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
                  You can do this in any loop.

                  For example, no error checking done

                  Code:
                  char string[256];
                  int x;
                  
                  cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b
                  
                  while (string(x) <> "\0") {
                  x++;
                  }
                  
                  cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
                  Hope this helps

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Thre previous 9 posts are riddled with errors:

                    Originally:
                    Originally posted by goelvivek
                    write a program to find the length of the string without using control structures and without using string.h header files???
                    Using the string.h header file indicates that the OP is interested in C strings which makes the following seem odd:

                    Originally posted by PuzzleC
                    what is a type C string?
                    C++ string
                    approximate:

                    Code: ( text )
                    cpp
                    class string
                    {
                    private:
                    struct ChatT
                    {
                    ...
                    ...
                    blablabla;
                    };
                    ChatT *s;
                    int __length; //<---- private mamber of class string
                    public:
                    int length() { return this->__length; }
                    } ;
                    since there are no classes in C. This is a C++ example, and a poor one, since there is a C++ string class that is part of the C++ Standard Library. But all of that is irrelevant since the OP is interested in a C string.

                    A C string is an array of char with the last element a binary 0 ('\0').

                    Originally posted by Studlyami
                    when you say the length of the string, does string refer to a variable of string type or is this a character array?
                    The length of a C string is the number of characters counting from the start of the array to (but not including) the \0.

                    Originally posted by chazzy69
                    If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
                    You can do this in any loop.

                    For example, no error checking done


                    Code: ( text )
                    char string[256];
                    int x;

                    cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b

                    while (string(x) <> "\0") {
                    x++;
                    }

                    cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
                    There are a lot of errors in this code sample:
                    1) cin >> string will only fetch a string that has no whitespace in it. Usually, this means you fetch only one word of the input.

                    2) string[x] = "\0" won't compile. string[x] is a char. "\0" is an empty string. That is, an array with \0 as element[0]. In C and C++ the array name is the address of element[0] so this compares a char to the address of the char \0. The compiler will gag on this.

                    3) while (string(x) <> "\0") { won't compile. string(x) is a function call to the function string using the argument x. Proably you meant string[x]. However the <> won't work, again that compares a char to the addess of a char. See (2) above.

                    Now read my Post #5 again and follow the direction exactly.

                    Comment

                    Working...