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
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!";
}
Discussions

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
enter and isspace - C++ Forum
I've begun reading a bit about c type string functions and I've come across this strange behavior in my code: the isspace() function I've used in the line before the last line is not working as I expect. For example I give this input "mbn1484[enter]" but isspace believes I've not entered any ... More on cplusplus.com
🌐 cplusplus.com
How do I make my program ignore spaces at the very beginning or very end of a string?
Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed. Read our guidelines for how to format your code. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/cpp_questions
4
1
February 21, 2023
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
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › isspace
std::isspace - cppreference.com
#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'; }
🌐
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 ?

🌐
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.
🌐
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 ·
🌐
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:
🌐
Cplusplus
cplusplus.com › reference › cctype › isspace
isspace
Checks whether c is a white-space character.
Find elsewhere
🌐
W3cubDocs
docs.w3cub.com › cpp › string › byte › isspace
Std::isspace - C++ - W3cubDocs
int count_spaces(const std::string& s) { return std::count_if(s.begin(), s.end(), // static_cast<int(*)(int)>(std::isspace) // wrong // [](int c){ return std::isspace(c); } // wrong // [](char c){ return std::isspace(c); } // wrong [](unsigned char c){ return std::isspace(c); } // correct ); }
🌐
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); } Output: 0x09 0x0a 0x0b 0x0c 0x0d 0x20 ·
🌐
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 - Application: isspace() function is used to find number of spaces in a given sentence. Examples: Input : This is a good website Output : Number of spaces in the sentence is : 4 Input : heyy this is geeksforgeeks Output : Number of spaces in the ...
🌐
Programiz
programiz.com › c-programming › library-function › ctype.h › isspace
C isspace() - C Standard Library
In C programming, isspace( ) checks whether a character is white-space character or not. If a character passed to isspace( ) is white-space character, it returns non-zero integer if not it returns 0.
🌐
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 = ...
🌐
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.
🌐
cppreference.com
en.cppreference.com › cpp › locale › isspace
std::isspace(std::locale) - cppreference.com
Demonstrates the use of std::isspace() with different locales (OS-specific). Run this code · #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"); } Possible output: isspace(' ', locale("C")) returned false isspace(' ', locale("en_US.UTF8")) returned true ·
🌐
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
Each of these routines returns nonzero if c is a particular representation of a space character. isspace returns a nonzero value if c is a white-space character (0x09 - 0x0D or 0x20).
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_isspace.htm
C Library - isspace() function
The C library isspace() function checks whether the passed character is white-space. Standard white-space characters are − Following is the C library syntax of the isspace() function − This function accepts a single parameter − The isspace(int ...
🌐
Programiz
programiz.com › cpp-programming › library-function › cctype › isspace
C++ isspace() - C++ Standard Library
The isspace() function returns non zero value if ch is a whitespace character, otherwise returns zero. #include <cctype> #include <iostream> #include <cstring> using namespace std; int main() { char str[] = "<html>\n<head>\n\t<title>C++</title>\n</head>\n</html>"; cout << "Before removing ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 234577
enter and isspace - C++ Forum
I've begun reading a bit about c type string functions and I've come across this strange behavior in my code: the isspace() function I've used in the line before the last line is not working as I expect. For example I give this input "mbn1484[enter]" but isspace believes I've not entered any whitespace characters!
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009696599 › functions › isspace.html
isspace
The c argument is an int, the value ... If the argument has any other value, the behavior is undefined. The isspace() function shall return non-zero if c is a white-space character; otherwise, it shall return 0....