I am studying strings and found that string ends with a null character – "\0". Is it possible to change this default null character by something else like "\1"?
I was performing the following code –
#include<stdio.h>
#include<string.h>
int main(){
const char *str = "App\0le";
printf("%c", *(str+5));
}
>Solution :
Yes and no. You can use any sentimel in your char arrays but but it will not be used in string literals. You will have to write your own string functions (file too).
size_t mystrlen(const char *str)
{
const char *end = str;
while(*end != '\1') end++;
return end - str;
}
int main(void)
{
char *str = "12345\0006789\01"; // it will still append \0 to the end
printf("%zu\n", mystrlen(str));
}