Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
main()
{
char str[20] = "SINGLE\0*DOUBLE*";
printf("8th character is %c\n", str[8]);
}


Hi, actually i need to decode the string after the null character but i got upto the null character string(only accessible upto SINGLE). i do not know how to solve the issue, please help it.

What I have tried:

used strcpy, snprintf for initializing the variable
Posted
Updated 12-Jul-22 23:11pm
v2
Comments
KarstenK 13-Jul-22 4:16am    
Because you have an 0 termination that data isnt a string per definition. You are jiggling around with the chance of nasty bugs.

In C you have to use a function, like memchr that doesn't consider '\0' character the end-of-the-string. Try, for instance
C
#include <stdio.h>
#include <string.h>
int main()
{
  char str[] = "SINGLE\0*DOUBLE*";
  char * p = str;

  for(;;)
  {
    size_t remain = sizeof(str) - (p - str + 1);
    if ( ! remain )
      break;
    printf("%s\n", p);
    p = memchr(p, 0, remain);
    if ( ! p )
      break;
    ++p;
  }

  return 0;
}
 
Share this answer
 
Comments
Patrice T 13-Jul-22 4:11am    
+5
Quote:
How to decode the string after the null character?

By C definition, a string is a zero terminates array of chars.
This means that all common C string functions will stop at the zero.

Run this code:
C++
void main() {
    char str[20] = "SINGLE\0*DOUBLE*";
    printf("8th character is %c\n", str[8]);
    for (int i=0; i<20; i++) {
        printf("value:%i char:'%c' string: '%s'\n", (int)str[i], str[i], &str[i]);
    }
}
 
Share this answer
 
v2
Comments
CPallini 13-Jul-22 4:37am    
5.
Patrice T 13-Jul-22 4:47am    
Thank you
If you are certain that there are only two words then it is fairly simple:
C++
char str[20] = "SINGLE\0*DOUBLE*";
char* first = str;
char* second = first + strlen(first) + 1;

printf("First: %s, Second: %s\n", first, second);

If you expect multiple entries then it is a good idea to terminate the final entry with an extra null character:
C++
char list[] = "first\0second\0third\0\0"
 
Share this answer
 
Comments
CPallini 13-Jul-22 4:38am    
5.
Hi,
may be you can do a replace code than delete the null character before your printf

for(int i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == "\O")  
		{
  			str[i] = "";
 		}
	}
 
Share this answer
 
Comments
CPallini 13-Jul-22 4:38am    
How do you hope to find '\0' in the 0..strlen(str) range?
Ruyter64 13-Jul-22 4:55am    
you're right.
may be better to use strrchr() to search a special character and the with the position found replace it by other character.
CPallini 13-Jul-22 5:02am    
Nope, as correctly stated by Patrice: "This means that all common C string functions will stop at the zero.". You have to use a different kind of function (like, for instance, the one I used: memchr),

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900