How can I handle string if null character in the middle of string?

2.4k Views Asked by At

I understand that string ends with a NULL. But if there is a null character(\0) in the middle of string, how can I handle the string?

#include<stdio.h>
#include<string.h>
int main(){
char *str = "App\0le";
char *str2;

printf("%c", *(str+5));
}

output: e

  1. string is ends with null character(\0), how can "e" be output?
  2. Without function in <string.h>, how can i assign apple using str1?
4

There are 4 best solutions below

0
user253751 On

You can't have a null character in the middle of a C string, because a null character, by definition, ends the string.

You can use arrays of chars where some of them are null characters, but you have to treat them as arrays, not strings. So you have to keep track of the length yourself.

1
Antonin GAVREL On
  1. You are accessing 'e' using pointer arithmetic.

  2. You can recode strdup, but you have to keep track of the length of str because by definition strings are null-terminated:

str2 = malloc(7 * sizeof(char)); // I let you handle protection
for (int i = 0; i < 7; i++) { // 7 to include the final \0
   str2[i] = str[i];
}

check with:

printf("%c", *(str2+5));

output:

e
0
Vlad from Moscow On

string is ends with null character(\0), how can "e" be output?

The string literal "App\0le" is stored in memory as an unnamed character array having the following elements

char unnamed_string_literal[7] =  { 'A', 'p', 'p', '\0', 'l', 'e', '\0' };

This declaration

char *str = "App\0le";

may be rewritten taking into account the above assumption the following way

char *str = unnamed_string_literal;

So using the pointer arithmetic and knowing a priori the number of elements in the string literal (including its embedded zero character) you can output any elements of the character array that represents the string literal.

For example

#included <stdio.h>

int main( void )
{
    char *str = "App\0le";

    for (size_t i = 0; i < 7; i++)
    {
        if (str[i] == '\0')
        {
            putchar( '\\' ), putchar( '0' );
        }
        else
        {
            putchar( str[i] );
        }
    }

    putchar( '\n' );
}

The program output is

App\0le\0

That is the expression str[i] is an expression of accessing i-th element of an array. It is totally unimportant what the type of the array and what it stores.

If you will write

char *str2 = str;

then the pointer str2 will point to the first character of the same string literal pointed to by the pointer str.

If you need to get a string then you need to declare a character array as for example

char str2[6];

and copy to it characters of the string literal pointed to by the pointer str excluding the embedded zero character but including the terminating zero character. You may not change the string literal itself because any attempt to change a string literal results in undefined behavior.

For example (without using standard C string functions)

#include <stdio.h>

int main( void )
{
    char *str = "App\0le";
    char str2[6];

    size_t i = 0;

    while (( str2[i] = str[i] ) != '\0') i++;
    while (( str2[i] = str[i + 1] ) != '\0') i++;

    puts( str2 );
}

The program output is

Apple
0
chux - Reinstate Monica On

How can I handle string if null character in the middle of string?

The string literal "App\0le" (size 7) begins with a string "App" (size 4).

A string always ends with a null character as C library defines string as

A string is a contiguous sequence of characters terminated by and including the first null character.

But "App\0le" is a string literal

'A' 'p' 'p' '\0' 'l' 'e' '\0'

With OP's code, str only retains the address of the string literal and not its size.

char *str = "App\0le";  // str is a pointer

We need some way to gather more information about "App\0le" than just using its address.


string is ends with null character(\0), how can "e" be output?

The string "App" does end before the 'e', yet string literal "App\0le" is accessible further on.


how can i assign apple using str1?

Consider using an array

char str_alt[] = "App\0le";  // str_alt is an array

str_alt contains "App\0le" and the size of str_alt is 7.

// Assign through copy
char str_copy[sizeof str_alt];

memcpy(str_copy, str_alt, sizeof str_alt);
// Or equivalent code
for (size_t i = 0; i < sizeof str_alt; i++) {
  str_copy[i] = str_alt[i];
}

To make "Apple" only, selectively copy

char str_copy2[sizeof str_alt];
size_t dest_i = 0;
for (size_t i = 0; i < sizeof str_alt; i++) {
  if (str_alt[i]) {
    str_copy2[dest_i++] = str_alt[i];
  }
}
str_copy2[dest_i] = '\0';