A method of determining whether a character is a blank or special character in C language _c language

Source: Internet
Author: User

C language Isspace () function: To determine whether a character is a white space character
header file:

#include <ctype.h>

To define a function:

int isspace (int c);

Function Description: Check that parameter C is a space character, that is, whether it is a space ('), a positional character (' \ t '), a CR (' \ R '), a newline (' \ n '), a vertical positioning character (' \v '), or a page (' \f ').

Return value: If the parameter C is a white-space character, it returns a non-0, otherwise it returns 0.

Additional description: This is a macro definition, not a true function.

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

#include <ctype.h>
Main () {
  char str[] = "123c @# fd\tsp[e?\n";
  int i;
  for (i = 0; Str[i]!= 0. i++)
    if (Isspace (str[i)))
      printf ("str[%d" is a white-space character:%d\n ", 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 language Ispunct () function: To determine whether a character is a punctuation mark or special character
header file:

#inlude <ctype.h>

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

  int ispunct (int c);

"Parameter" C is the character you want to detect.

Return value if C is a punctuation mark or special symbol (non-space, non-numeric, and non-English letters) returns a value other than 0, or 0.

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

"Instances" lists punctuation marks or special symbols in 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.