All that is very unnecessary to just test if a string has a space in it. This is all you need:

#include <ctype.h>

bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end();

:: is the scope resolution operator specifying that isspace is a global function, not the similarly-named std::isspace, and find_if is a function inside std::. If you use using namespace std; then you don't need std:: but you do still need the plain ::.

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

That means that if find_if returns str.end(), it got to the end of the string without isspace returning true, which means there was no space characters in the string. Therefore, you can test the result of find_if against str.end(); If they are unequal (!=), that means there was a space in the string, and hasspace is true. Else, hasspace is false.

Answer from Seth Carnegie on Stack Overflow
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › isspace
std::isspace - cppreference.com
bool my_isspace(char ch) { return std::isspace(static_cast<unsigned char>(ch)); }
🌐
Cplusplus
cplusplus.com › reference › cctype › isspace
isspace
Checks whether c is a white-space character.
Discussions

How to use the isspace function in c++? - Stack Overflow
Techinically, we havn't learned any of this "isspace" or "isdigit" stuff, this is me googling ahead and finding this things. I'll take your advice and try and "unlearn c" when learning C++. (: ... Save this answer. ... Show activity on this post. All that is very unnecessary to just test if a string has a space in it. This is all you need: #include bool hasspace = std... More on stackoverflow.com
🌐 stackoverflow.com
Why I cannot put std in front of ::isspace ?
Like u/fortsnek47 said, there are two versions of that function, one of which is in the global namespace (::isspace). This SO answer might be relevant for you: https://stackoverflow.com/questions/46208733/in-c-why-does-cctype-define-both-stdisspace-and-isspace More on reddit.com
🌐 r/cpp_questions
3
7
December 18, 2021
Why isspace dont register '\n' (c++)?
Just to rule out the obvious, have you checked that arr actually has a '\n' at the end? More on reddit.com
🌐 r/AskProgramming
7
5
March 18, 2021
How do I store user input (with spaces) in a string?
>> stops at whitespace. It needs some way to seperate inputs after all. The easiest way is to use std::getline instead: std::getline( std::cin, userinput ); More on reddit.com
🌐 r/cpp_questions
8
9
August 11, 2023
🌐
Cplusplus
cplusplus.com › reference › locale › isspace
std::isspace - locale
Checks whether c is a white-space character using the ctype facet of locale loc, returning the same as if ctype::is is called as:
🌐
cppreference.com
en.cppreference.com › c › string › byte › isspace
isspace - cppreference.com
#include <ctype.h> #include <limits.h> #include <stdio.h> int main(void) { for (int ndx = 0; ndx <= UCHAR_MAX; ++ndx) if (isspace(ndx)) printf("0xx ", ndx); }
🌐
Cppreference
en.cppreference.com › w › cpp › string › wide › iswspace
std::iswspace - cppreference.com
#include <clocale> #include <cwctype> #include <iostream> void try_with(wchar_t c, const char* loc) { std::setlocale(LC_ALL, loc); std::wcout << "isspace('" << c << "') in " << loc << " locale returned " << std::boolalpha << static_cast<bool>(std::iswspace(c)) << '\n'; } int main() { const wchar_t EM_SPACE = L'\u2003'; // Unicode character 'EM SPACE' try_with(EM_SPACE, "C"); try_with(EM_SPACE, "en_US.UTF8"); } Output: isspace(' ') in C locale returned false isspace(' ') in en_US.UTF8 locale returned true ·
🌐
Cppreference
en.cppreference.com › w › cpp › locale › isspace.html
std::isspace(std::locale) - cppreference.com
#include <iostream> #include <locale> void try_with(wchar_t c, const char* loc) { std::wcout << "isspace('" << c << "', locale(\"" << loc << "\")) returned " << std::boolalpha << std::isspace(c, std::locale(loc)) << '\n'; } int main() { const wchar_t EM_SPACE = L'\u2003'; // Unicode character 'EM SPACE' try_with(EM_SPACE, "C"); try_with(EM_SPACE, "en_US.UTF8"); }
Top answer
1 of 2
15

All that is very unnecessary to just test if a string has a space in it. This is all you need:

#include <ctype.h>

bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end();

:: is the scope resolution operator specifying that isspace is a global function, not the similarly-named std::isspace, and find_if is a function inside std::. If you use using namespace std; then you don't need std:: but you do still need the plain ::.

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

That means that if find_if returns str.end(), it got to the end of the string without isspace returning true, which means there was no space characters in the string. Therefore, you can test the result of find_if against str.end(); If they are unequal (!=), that means there was a space in the string, and hasspace is true. Else, hasspace is false.

2 of 2
0

here is another way, if the above version seems strange, or it's above your knowledge

if(marks[i] == ' ') {
cout<<"Space found!";
}
🌐
Programiz
programiz.com › c-programming › library-function › ctype.h › isspace
C isspace() - C Standard Library
The isspace() function is defined in ctype.h header file. List of all white-space characters in C programming are: #include <stdio.h> #include <ctype.h> int main() { char c; int result; printf("Enter a character: "); scanf("%c", &c); result = isspace(c); if (result == 0) { printf("Not a white-space character."); } else { printf("White-space character."); } return 0; } Output ·
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › isspace-in-c
isspace() in C - GeeksforGeeks
September 13, 2024 - It is the character to be tested. The isspace() function returns an integer value that tells whether the passed parameter is a whitespace character or not.
🌐
SACO Evaluator
saco-evaluator.org.za › docs › cppreference › en › cpp › string › byte › isspace.html
std::isspace - cppreference.com
Checks if the given character is whitespace character as classified by the currently installed C locale. In the default locale, the whitespace characters are the following: · The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF
🌐
W3cubDocs
docs.w3cub.com › cpp › string › byte › isspace
Std::isspace - C++ - W3cubDocs
bool my_isspace(char ch) { return std::isspace(static_cast<unsigned char>(ch)); }
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › isspace-iswspace-isspace-l-iswspace-l
isspace, iswspace, _isspace_l, _iswspace_l | Microsoft Learn
Determines whether an integer represents a space character. int isspace( int c ); int iswspace( wint_t c ); int _isspace_l( int c, _locale_t locale ); int _iswspace_l( wint_t c, _locale_t locale );
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_isspace.htm
C Library - isspace() function
A character 't', a number '1' and a space '' are checked for whitespace recognition using the isspace() function. #include <stdio.h> #include <ctype.h> int main () { int var1 = 't'; int var2 = '1'; int var3 = ' '; if( isspace(var1) ) { printf("var1 = |%c| is a white-space character\n", var1 ); } else { printf("var1 = |%c| is not a white-space character\n", var1 ); } if( isspace(var2) ) { printf("var2 = |%c| is a white-space character\n", var2 ); } else { printf("var2 = |%c| is not a white-space character\n", var2 ); } if( isspace(var3) ) { printf("var3 = |%c| is a white-space character\n", var3 ); } else { printf("var3 = |%c| is not a white-space character\n", var3 ); } return(0); } The above code produces following result− ·
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009696599 › functions › isspace.html
isspace
Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. The isspace() function shall test whether c is a character of class space in the program's current locale; see the Base Definitions ...
🌐
Lsu
ld2015.scusa.lsu.edu › cppreference › en › cpp › string › wide › iswspace.html
std::iswspace - cppreference.com
#include <iostream> #include <clocale> #include <cwctype> void try_with(wchar_t c, const char* loc) { std::setlocale(LC_ALL, loc); std::wcout << "isspace('" << c << "') in " << loc << " locale returned " << std::boolalpha << (bool)std::iswspace(c) << '\n'; } int main() { wchar_t EM_SPACE = ...
🌐
GeeksforGeeks
geeksforgeeks.org › isspace-in-c-and-its-application-to-count-whitespace-characters
isspace() in C/C++ and its application to count whitespace characters - GeeksforGeeks
March 3, 2023 - Notes ... isspace() function In C++, isspace is a predefined function used for string and character handling.cstring is the header file required for string functions and cctype is the headerfile required for character functions.
🌐
Melashri
melashri.net › cpp › cpp › string › byte › isspace
std::isspace | Modern C/C++ Reference
#include <cctype> #include <climits> #include <iomanip> #include <iostream> int main(void) { std::cout << std::hex << std::setfill('0') << std::uppercase; for (int ch{}; ch <= UCHAR_MAX; ++ch) if (std::isspace(ch)) std::cout << std::setw(2) << ch << ' '; std::cout << '\n'; }
🌐
Cppreference
cppreference.dev › w › cpp › string › byte › isspace
std::isspace - cppreference.com - C++ Reference
bool my_isspace(char ch) { return std::isspace(static_cast<unsigned char>(ch)); }
🌐
Cppreference
cppreference.net › cpp › string › byte › isspace.html
std::isspace - cppreference.net
bool my_isspace(char ch) { return std::isspace(static_cast<unsigned char>(ch)); }
🌐
Reddit
reddit.com › r/cpp_questions › why i cannot put std in front of ::isspace ?
r/cpp_questions on Reddit: Why I cannot put std in front of ::isspace ?
December 18, 2021 -

Hi everyone,

I recently watched the CppCon talk "C++20 Ranges in Practice" (https://youtu.be/d_E-VLyUnzc?t=3516) and there is something I did not understand.

In an example he gave, why did he omits the std before the ::isspace in this line: inline constexpr auto trim_front = views::drop_while(::isspace); When I try to put the std, the compiler gives me an error in this Compiler Explorer example (https://godbolt.org/z/sfnKnfvnP)

I tried to search why on internet but I don't even know what I should search ^^

Could you give me some insights about that pls ?