How to use isspace function in C ++?

I am trying to figure out how to use this feature. I found it on the Internet and it seems to be checking if you have a space in your line. So this does not work for me. I realized that I am not even going into the if statement I need.

for (i=0;i < marks.length();i++) { if (isdigit(marks[i])) { floatMARK = 1; } else { charMARK = 1; } } if (floatMARK == 1) { printf("were in."); for (i=0;i < marks.length();i++) { if (isspace(marks[i])) { multiMARK = 1; printf("WE HAVE A SPACE!!"); } } } 

Does anyone know what I'm doing wrong? If you need me to clarify something, let me know.

+4
source share
2 answers

All that really does not need to just check whether there is a gap in it. This is all you need:

 #include <ctype.h> bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end(); 

:: is a scope resolution statement indicating that isspace is a global function, not the so-called std::isspace and find_if is a function inside std:: . If you are using namespace std; , you do not need std:: , but you still need a simple :: .

The find_if function takes an iterator at the beginning of a line, an iterator at the end of a line, and a function that takes an argument and returns some value convertible to bool . find_if from the first iterator to the second iterator, passing each value of the current element of the function that you gave it, and if the function returns true , find_if returns an iterator that caused the function to return true . If find_if passes to the end, and the function never returns true , it returns an iterator to the end of the range, which in this case is equal to str.end() .

This means that if find_if returns str.end() , it isspace to the end without isspace returning true , which means there were no spaces in the line. Therefore, you can check the result of find_if on str.end() ; If they are not equal ( != ), This means that there is a space in the line, and hasspace is true . Else, hasspace - false .

+12
source

here is another way if the above version seems weird or above your knowledge

 if(marks[i] == ' ') { cout<<"Space found!"; } 
0
source

Source: https://habr.com/ru/post/1392393/


All Articles