I have weird string that I want to inspect by printing its characters one by one. How can this be done?
I'm worried in case it has any special c开发者_运维百科haracters that may obstruct its printing. Can they be 'escaped'?
You could loop over the string, printing the characters one by one, and conditionally choosing to print the character or an escape sequence:
char *str, // the original string
*tmp;
for(tmp = str; *tmp; tmp++)
{
printf((iscntrl(*tmp) ? "%02x\n" : "'%c'\n"), *tmp);
}
This prints one character per line, with control characters printed in hex format.
An alternative way to see special characters:
./a.out | hexdump -C
hexdump
#include<stdio.h>
main()
{
int i, count=0;
char c[30];
printf("Enter a Char string:");
scanf("%s", &c[i]);
for(count=0; count<c[30]; )
{
printf("%c", c[i]);
count=+2;
}
return 0;
}
精彩评论