C++ isspace()

isspace() Prototype

int isspace(int ch);

The isspace() function checks if ch is a whitespace character as classified by the current C locale. By default, the the following characters are whitespace characters:

  • space (0x20, ' ')
  • form feed (0x0c, '\f')
  • line feed (0x0a, '\n')
  • carriage return (0x0d, '\r')
  • horizontal tab (0x09, '\t')
  • vertical tab (0x0b, '\v')

The behaviour of isspace() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

isspace() Parameters

ch: The character to check.

isspace() Return value

The isspace() function returns non zero value if ch is a whitespace character, otherwise returns zero.

Example: How isspace() function works

#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 whitespace characters" << endl;
    cout << str << endl << endl;

    cout << "After removing whitespace characters" << endl;
    for (int i=0; i<strlen(str); i++)
    {
        if (!isspace(str[i]))
            cout << str[i];
    }
    
    return 0;
}

When you run the program, the output will be:

Before removing whitespace characters
<html>
<head>
	<title>C++</title>
<head>
<html>

After removing whitespace characters
<html><head><title>C++</title></head></html>