C language to determine whether a character is a blank character or a special character

  • 2020-04-02 03:16:54
  • OfStack

C isspace() function: determines whether a character is a blank character
The header file:


#include <ctype.h>

Definition function:


int isspace(int c);

Function description: check whether the parameter c is a space character, that is, judge whether it is a space ('), positioning character (' \t '), CR(' \r '), line feed (' \n '), vertical positioning character (' \v ') or page turn (' \f ').

Return value: returns non-0 if c is a blank character, or 0 if not.

Note: this is a macro definition, not a real function.

Example: find the space character contained in the string STR [] and display the ASCII code for the space character.


#include <ctype.h>
main(){
  char str[] = "123c @# FDtsP[e?n";
  int i;
  for(i = 0; str[i] != 0; i++)
    if(isspace(str[i]))
      printf("str[%d] is a white-space character:%dn", i, str[i]);
}

Execution results:


str[4] is a white-space character:32
str[7] is a white-space character:32
str[10] is a white-space character:9 // t
str[16] is a white-space character:10 // t

C ispunct() function: determines whether a character is a punctuation mark or a special character
The header file:


#inlude <ctype.h>

The ispunct() function is used to detect whether a character is a punctuation mark or a special character, and its prototype is:


  int ispunct(int c);

[parameter] c is the character to be detected.

Returns a nonzero value if c is a punctuation mark or a special symbol (not a space, not a number, or not a letter), otherwise returns zero.

Note that this is a macro definition, not a real function.

Example: lists punctuation or special symbols in the string STR.


#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  int cx=0;
  char str[]="Hello, welcome!";
  while (str[i])
  {
    if (ispunct(str[i])) cx++;
    i++;
  }
  printf ("Sentence contains %d punctuation characters.n", cx);
  return 0;
}

Output results:


Sentence contains 2 punctuation characters.


Related articles: