0

Is there any C library function that copies a char array (that contains some '\0' characters) to another char array, without copying the '\0'?
For example, "he\0ll\0o" should be copied as "hello".

Yoghi
  • 49
  • 8
  • Can you post some of your code? Do you define these strings, because, if you do, it is certainly bad design to include string terminators in the middle of the string. This shows that you do not really understand how strings or, to be precise, `char` arrays work in C. – Lysandros Nikolaou Apr 28 '17 at 12:14
  • It is not that I want to include string terminators inside, but I have to, I have been asked to do so, though it is bad design. – Yoghi Apr 28 '17 at 12:24
  • Do you know how long the char array is? – Neil Apr 28 '17 at 12:35
  • @Neil yes, I know the size. – Yoghi Apr 28 '17 at 12:36

3 Answers3

3

As long as you know how long the char array is then:

void Copy(const char *input, size_t input_length, char *output)
{
  while(input_length--)
  { 
     if(input!='\0')
        *output++ = input;
     input++;
   }
   *output = '\0'; /* optional null terminator if this is really a string */
}

void test()
{
     char output[100];
     char input = "He\0ll\0o";
     Copy(input, sizeof(input), output);
 }
Neil
  • 11,059
  • 3
  • 31
  • 56
0

No, there is no library function that does that. You have to write your own.

One question though: how do you know when to stop ignoring \0? Your string ("he\0ll\0o") has three zero bytes. How do you know to stop at the third?

pmg
  • 106,608
  • 13
  • 126
  • 198
0

'\0' in strings is a method to find the end of the string(terminating character for strings). So, all the functions designed for string operations use '\0' to detect end of string.

Now, if you want such implementation as you have asked, you need to design of your own.
Problem you will face is:
1) how will you determine which '\0' is used as a terminating character?
So for such implementation, you need to explicitly tell the count of '\0' used as terminating character or you need to set your own terminating character for strings.
2) For any other operations on your implementation, you cannot use predefined string related functions.
So, Implement your own functions to do those operations.

anil
  • 158
  • 1
  • 12