Home > database >  Need to print all string
Need to print all string

Time:12-29

I have a string with multiple \0's and I need to output the whole string. `

#include <stdio.h>

int main() {
  char *arr = "asdf\0\0zxcvb\0"; // for example
    for (int i = 0; arr[i]; i  ) {
      printf("%c", arr[i]);
    }
    return 0;
}

` Help me come up with a solution.

I have no idea what should I do.

CodePudding user response:

This code

char *arr = "asdf\0\0zxcvb\0";

gives you a pointer to a string literal (aka a const char array).

When you only have a pointer, there is no way to get the size of the whole array when it contains \0 in the middle. The "normal" way would be to use strlen(arr) but that won't work for this case as it will stop at the first \0.

Consequently it is impossible to print it as an unknow number of traditional zero-terminated strings. The problem is that you can't know when to stop.

For something like that to work, you'll need extra information (e.g. array size or total number of strings in the array) or have some special rules (e.g. a sentinel value to indicate end-of-array).

CodePudding user response:

This will print out the entire string and not stop at the null characters. As it continues until i is equal to or greater than the sizeof arr

#include <stdio.h>

int main() {
  char arr[] = "asdf\0\0zxcvb\0"; // for example
  int i = 0;
  while (i < sizeof(arr)) {
    printf("%c", arr[i]);
    i  ;
  }
  return 0;
}

Output:

asdfzxcvb

Alternatively you could also use fwrite

#include <stdio.h>

int main() {
  char arr[] = "asdf\0\0zxcvb\0"; // for example
  int len = sizeof(arr);
  fwrite(arr, 1, len, stdout);
  return 0;
}

CodePudding user response:

You can't print it entirely, unless you know it's actual size.

If one (or multiples) \0 lay in your string, you can't use it to know where string's end belong. Your only way is to know the string's total size like:

for (int i = 0; i < size; i  ) {
  printf("%c", arr[i]);
}

This method is used while displaying raw binary, who often have multiple \0 occurrence in their content, but total binary size is known so it's not much a problem.

Another way of handling this is to use C and std::vector type, which can be used as string, but with a length property so you can easily have multiple \0.

CodePudding user response:

As mentioned in the above answers, there's no possible way to compute the size of an array of characters with embedded null characters with only a pointer.

But if you do have the size, you could get hacky:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
   const char *arr = "asdf\0\0zxcvb\0"; 
   for (size_t i = 0; i < 12; i  ) {
        if (arr[i] == 0) {
            printf("\\0");
        } else {
         printf("%c", arr[i]);
        }
   }
   return EXIT_SUCCESS;
}

Sample Output:

asdf\0\0zxcvb\0

Or perhaps:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
   /* Escape the null bytes */
   const char *arr = "asdf\\0\\0zxcvb\\0"; 
   size_t len = strlen(arr);

   for (size_t i = 0; i < len; i  ) { 
        printf("%c", arr[i]); 
   }
   return EXIT_SUCCESS;
}

Sample Output:

asdf\0\0zxcvb\0

CodePudding user response:

The loop in your code will terminate when it encounters a null character ('\0') in the string. This is because the condition arr[i] will be false when arr[i] is a null character.

To output the entire string, you can use a different loop condition that does not depend on the null character. For example, you can use the following code:

#include <stdio.h>
#include <string.h>

int main() {
  char *arr = "asdf\0\0zxcvb\0"; // for example
  int len = strlen(arr);
  for (int i = 0; i < len; i  ) {
    printf("%c", arr[i]);
  }
  return 0;
}

Or

#include <stdio.h>

int main() {
  char *arr = "asdf\0\0zxcvb\0"; // for example
  int i = 0;
  while (1) {
    printf("%c", arr[i]);
    if (arr[i] == '\0') {
      break;
    }
    i  ;
  }
  return 0;
}
  •  Tags:  
  • c
  • Related