0

I'm trying to figure out how to know if there is a space in a string. Since in lecture it was said that in C a blank is "\0", I figured that comparing the array item with it could work.

For example, if I would like to know if a string starts with a blank, I'm trying out this code:

string name = GetString(); if (name[0] != '\0'){ printf("The string starts with a blank space\n"); }

It compiles, but it doesn't work. What's the problem with this statement: (name[0] != '\0')?

Thank you.

1 Answer 1

3

I think that you misinterpreted or misheard something, or took something out of context. Characters in strings are stored in a computer with their numeric ASCII values. A space is not 0, it's 32. The \0 is the end of string marker, not a space, and is stored in the computer as the number 0, also known as NULL. (Google "ASCII TABLE" and look it over. )

Also, you might want to look up the function isspace(). You might find it useful. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks Cliff! Very helpful.
    – Rodulfo
    Commented Sep 12, 2016 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .