C++ Standard Library C++ STL Library

C++ <cctype> - isgraph() Function



The C++ <cctype> isgraph() function is used to check if the given character has a graphical representation. A character with graphical representation are those characters than can be printed (as determined by isprint) except the space character (' '). In the default "C" locale, the following characters are graphic:

  • Digits (0123456789)
  • Uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • Lowercase letters (abcdefghijklmnopqrstuvwxyz)
  • Punctuation characters (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)

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

Syntax

int isgraph ( 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 has a graphical representation, else returns zero (i.e, false).

Example:

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

#include <iostream>
#include <cctype>
using namespace std;
 
int main (){
  char str[50] = "Hello \nWorld!";

  //replacing all characters which has no 
  //graphical representation with @ in str
  int i = 0;
  while(str[i]) {
    if(!isgraph(str[i]))
      str[i] = '@';
    i++;
  }

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

The output of the above code will be:

str contains: Hello@@World!

❮ C++ <cctype> Library

5