0

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 characters that may obstruct its printing. Can they be 'escaped'?

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

An alternative way to see special characters:

./a.out | hexdump -C

hexdump

2 Comments

How comes everybody implies Linux is at hand? Is Linux that widespread today?
@TheBlastOne: this is not a Linux-specific answer - it will work with Linux, Unix, Mac OS X, any POSIX OS, and even with Windows if you use something like cygwin.
1
#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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.