+ 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
5 Answers
+ 11
if(str[i] == ' ')
+ 7
You need to use single quotes for chars and double quotes for strings
+ 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
+ 1
Why didn't double quotes work?
+ 1
[meta, note] Chars from strings are numeric* so this also works.
if(str[i]!= 32) {
* signed