Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to change the default null character at the end of string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading