+ 2

Check for spaces?

How can I check if the current character is a space or not? I need to print "space" if there's a space. I couldn't get an if statement to work. https://code.sololearn.com/c4ijn3MBIiP6/?ref=app

31st Oct 2018, 6:23 AM
Daniel Cooper
Daniel Cooper - avatar
5 Antworten
+ 11
if(str[i] == ' ')
31st Oct 2018, 6:32 AM
Anna
Anna - avatar
+ 7
You need to use single quotes for chars and double quotes for strings
31st Oct 2018, 6:38 AM
Anna
Anna - avatar
+ 5
An alternative, add cctype header; #include <cctype> Then, inside your loop, check for whitespace with std::isspace() to verify whether the said character is classified as a whitespace. if(std::isspace(str[i]) // whitespace character else // non whitespace Reference: https://en.cppreference.com/w/cpp/string/byte/isspace
31st Oct 2018, 6:46 AM
Ipang
+ 1
Why didn't double quotes work?
31st Oct 2018, 6:37 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
[meta, note] Chars from strings are numeric* so this also works. if(str[i]!= 32) { * signed
31st Oct 2018, 5:11 PM
Kirk Schafer
Kirk Schafer - avatar