Thread: Read string length when string contains the null character

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Read string length when string contains the null character

    Hi! I wonder how you can replace the null character to be able to read the length of the whole string. Because C only reads to the null character I was told so I have to replace it with something else.

    The reason why my for loop has 7 is temporarly, just to make sure it can read all elements. Since strlen(str) returns 3 because of the null character.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main() {
        
        char str[] = {'h', 'e', 'j', '\0', 'a', 'b', 'c'};
        
        printf("String: %s", str);
        
        int i; 
        for (i = 0; i < 7; i++) {
            if (strcmp(str, "\0") == 0)
                str[i] = ' ';
        }
            
        printf("\nLength: %d", strlen(str));
        
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    If your string has to allow for embedded null characters, then by the standard C definition of "string", it is not a string. As such, just treat it as you would any other array of items: if you only use a portion of the array, you need some way to keep track of the number of elements in use, so designate a variable to maintain that count. You should not be using any of the standard C functions pertaining to strings, no more than you would use them on an array of integers or an array of doubles (e.g., you could use memcpy as you would to copy an array of char that contains a string, or an array of int, or an array of double, but you would not use strcpy; likewise just as you wouldn't use printf with %s to print an array of double, you wouldn't use it to print your array of char, but perhaps you would use a loop containing printf with %c).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    5
    Hi! I wonder how you can replace the null character to be able to read the length of the whole string. Because C only reads to the null character I was told so I have to replace it with something else.

    The reason why my for loop has 7 is temporarly, just to make sure it can read all elements. Since strlen(str) returns 3 because of the null character.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    int main() {
    
    
        int i;
        char str[] = {'h', 'e', 'j', '\0', 'a', 'b', 'c'};
        int array_length = sizeof(str)/sizeof(str[0]);
        int characters_length = 0;
    
    
        for(i=0; i<array_length; i++) {
            if(str[i] == '\0') {
                str[i] = ' ';
            }
            printf("%c",str[i]); //The string character by character
        }
    
    
        /** alternative way to do it */
        printf("\nLength of array: %d\n\n\n", sizeof(str)/sizeof(str[0]));
        printf("%s",str); //The whole string
    
    
        characters_length = strlen(str); //assign string length to a variable
        printf("\nThe string length is: %d\n\n",characters_length); //print the length
    
    
    
    
        return 0;
    }
    C only reads to the null character
    Yes, you are right.
    Big thanks to paxdiablo for describing this. As he said @ StackOverflow: "strlen usually works by counting the characters in a string until a \0 character is found. A canonical implementation would be:

    Code:
    
    int len =0;
        while(str[i] != '\0'){
            len++;
        }
    
    
    I hope I helped. If you have any other question. Let me know!

  4. #4
    Registered User
    Join Date
    Jan 2018
    Posts
    5

    Solution!

    Here is how it should work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main() {
    
        int i;
        char str[] = {'h', 'e', 'j', '\0', 'a', 'b', 'c'};
        int array_length = sizeof(str); //Gets the array elements count no matter what.
        int characters_length = 0;
    
    
        for(i=0; i<array_length; i++) {
            if(str[i] == '\0') {
                str[i] = ' ';
            }
            printf("%c",str[i]); //The string character by character
        }
    
        characters_length = strlen(str); //assign string length to a variable
        printf("\nThe string length is: %d\n\n",characters_length); //print the length
    
        /** alternative way to do it */
        printf("\nLength of array: %d\n", sizeof(str));
        printf("%s\n\n\n",str); //The whole string
    
        return 0;
    }
    I hope I helped. If you have any question, let me know!
    Last edited by Mr Pro Pop; 01-06-2018 at 06:16 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    Quote Originally Posted by Mr Pro Pop
    Here is how it should work:
    No, your solution doesn't work: str is not a null terminated string, so replacing the embedded '\0' with ' ' and then calling strlen(str) results in undefined behaviour. In practice, strlen would try and find the null character and then go past the bounds of the array.

    The answer is quite simple in this instance: str is an array of char in which every character is in use, so sizeof(str) is the answer. For a more general solution, refer to my post #2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting length of UTF-8 character coded string?
    By Lazar in forum C Programming
    Replies: 4
    Last Post: 08-21-2015, 07:18 AM
  2. How to read character by character without string.h?
    By Jimmy King in forum C Programming
    Replies: 2
    Last Post: 10-29-2013, 01:00 AM
  3. Null character and string object
    By Jeb. in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2003, 11:12 PM
  4. need to read a string in one character at a time
    By mclain in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2002, 12:20 AM

Tags for this Thread