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
#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'; }
🌐
Cplusplus
cplusplus.com › reference › cctype › isspace
isspace
For a detailed chart on what the different ctype functions return for each character of the standard ASCII character set, see the reference for the <cctype> header. In C++, a locale-specific template version of this function (isspace) exists in header <locale>.
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
c++ - How to pass std::isspace to function taking unary predicate as argument? - Stack Overflow
BTW you cannot use std::isspace (and the other std::is... functions) on a char sequence; they take an int argument whose numeric value is expected to come from an unsigned char (IOW, you have to cast your char to unsigned char before calling std::isspace). 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
Does including <string> change the overload set of std::isspace?
secondary includes mostly vary between implementations. It is rarely specified in the standard explicitly that a certain header pulls certain over stuff in. That being said, you are not supposed to take the address of a function in the std namespace to begin with. This is something the standard explicitly calls out. Unless it makes an exception (for isspace, it does not) So no matter what your headers pull in, the correct implementation for you to use is something like this instead: auto is_non_space = [](unsigned char ch) { return not std::isspace(ch); }; Note that the character is required to be representable as unsigned char. More on reddit.com
🌐 r/cpp_questions
15
9
September 23, 2025
🌐
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 ...
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!";
}
🌐
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); }
🌐
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).
Find elsewhere
🌐
Pucrs
inf.pucrs.br › ~flash › progeng2 › cppreference › w › cpp › locale › isspace.html
std::isspace(std::locale) - Cppreference
Demonstrates the use of isspace() with different locales (OS-specific). #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"); } Output: isspace(' ', locale("C")) returned false isspace(' ', locale("en_US.UTF8")) returned true ·
🌐
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 ...
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › article › isspace-function-in-cplusplus
isspace() function in C++
March 11, 2026 - A program that implements isspace() function by counting the number of spaces in a string is given as follows − · #include <iostream> #include <ctype.h> using namespace std; int main() { char str[] = "Coding is fun"; int i, count = 0; for(i=0; str[i]!='\0';i++) { if(isspace(str[i])) count++; } cout<<"Number of spaces in the string are "<<count; return 0; }
🌐
SysTutorials
systutorials.com › docs › linux › man › 3-std::isspace
std::isspace: std::isspace - Linux Manuals (3)
checks if a character is classified as whitespace by a locale isspace(std::locale) (function template)
🌐
cpprefjp
cpprefjp.github.io › reference › cctype › isspace.html
std::isspace - cpprefjp C++日本語リファレンス
isspace · 最終更新日時: 2025年05月03日 12時01分40秒 (JST) rotarymars が更新 · 履歴 編集 · function · <cctype> namespace std { int isspace(int ch); } 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 ?

🌐
Reddit
reddit.com › r/cpp_questions › does including change the overload set of std::isspace?
r/cpp_questions on Reddit: Does including <string> change the overload set of std::isspace?
September 23, 2025 -

I am trialing the VS insider with some old code from a VS2017 project. I stumbled into a strange compilation error and after boiling it down to a minimal example on Compiler Explorer I found that it also generates an error on clang and gcc. I really want to understand if this code is actually incorrect or is this somehow a bug that all three vendors share (possibly in their libraries).

This code compiles:

#include <cctype>
#include <functional>

void test()
{
    auto is_non_space =  std::not_fn(std::isspace);
}

But if I just change it to include the string header ...

#include <cctype>
#include <functional>
#include <string>

void test()
{
    auto is_non_space =  std::not_fn(std::isspace);
}

Now the compilation fails with an error about not being able to determine the correct template substitution in not_fn. For example, clang 21.1.0 on compiler explorer gives

<source>:8:26: error: no matching function for call to 'not_fn'
    8 |     auto is_non_space =  std::not_fn(std::isspace);
      |                          ^~~~~~~~~~~
(long path)/include/c++/v1/__functional/not_fn.h:47:58: note: candidate template ignored: couldn't infer template argument '_Fn'
   47 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto not_fn(_Fn&& __f) {
      |                                                          ^

I can resolve the problem by dropping the "std::" qualifier from isspace

#include <cctype>
#include <functional>
#include <string>

void test()
{
    auto is_non_space =  std::not_fn(isspace);
}

After a little searching I see that there *is* a second std:isspace in the <locale> header and that would explain the compilation error, but I am not including locale in the failing example. So my questions are:

  • Does the <string> implementation include <locale> for some of these vendors?

  • If so, was that something that was changed since C++17?

  • If not, is there something else going on?

Top answer
1 of 4
12
secondary includes mostly vary between implementations. It is rarely specified in the standard explicitly that a certain header pulls certain over stuff in. That being said, you are not supposed to take the address of a function in the std namespace to begin with. This is something the standard explicitly calls out. Unless it makes an exception (for isspace, it does not) So no matter what your headers pull in, the correct implementation for you to use is something like this instead: auto is_non_space = [](unsigned char ch) { return not std::isspace(ch); }; Note that the character is required to be representable as unsigned char.
2 of 4
4
The specification of operator>> for string depends on isspace from ([string.io]/1.3), so it is not a surprise that vendors may make its declaration visible (as a side effect) while implementing operator<< in ([res.on.headers]/1). Leaked declarations in standard library headers are implementation details, and ideally we should not depend on their presence (or absence) when writing portable code. The root cause of this problem is that headers are a simplistic tool for providing declarations; with modules, we could, for example, write a module unit that wraps but only exposes public names (those intended for use). As for workarounds in your code, the explicit cast mentioned in u/FrostshockFTW 's comment should be fine in practice. However, as noted in u/n1ghtyunso 's comment, the behavior of forming an address or reference to a function that is not explicitly specified as addressable is unspecified ([namespace.std]/6). So to be pedantic, we need to wrap isspace inside a lambda expression (and this approach also lets us get rid of not_fn).
🌐
cppreference.com
en.cppreference.com › cpp › locale › isspace
std::isspace(std::locale) - cppreference.com
Checks if the given character is classified as a whitespace character by the given locale's std::ctype facet.
🌐
YouTube
youtube.com › watch
01. std::isspace (cctype) considered harmful
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.