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;
}