C++ Standard Library C++ STL Library

C++ <cctype> - isspace() Function



The C++ <cctype> isspace() function is used to check if the given character is a whitespace character. In the default "C" locale, the whitespace characters are the following:

  • Space (' ', 0x20)
  • Feed ('\f', 0x0c)
  • Newline ('\n', 0x0a)
  • Carriage return ('\r', 0x0d)
  • Horizontal tab ('\t', 0x09)
  • Vertical tab ('\v', 0x0b)

Other locales may consider a different selection of characters as whitespace characters.

Syntax

int isspace ( int ch );               

Parameters

ch Specify the character to be checked, casted to an int, or EOF.

Return Value

Returns non-zero value (i.e, true) if ch is a whitespace character, else returns zero (i.e, false).

Example:

The example below shows the usage of isspace() function.

#include <iostream>
#include <cctype>
using namespace std;
 
int main (){
  char str[50] = "To\rbe,\nor not to\tbe";

  //replacing the whitespace character
  //with new line character in str
  int i = 0;
  while(str[i]) {
    if(isspace(str[i]))
      str[i] = '\n';
    i++;
  }

  //displaying the output
  cout<<"str contains:\n"<<str;  
  return 0;
}

The output of the above code will be:

str contains:
To
be,
or
not
to
be

❮ C++ <cctype> Library