C language to determine whether a character is a printable character method _c language

Source: Internet
Author: User
Tags control characters printable characters

C language Isprint () function: To determine whether a character is a printable character
header file:

#include <ctype.h>

The Isprint () function is used to determine whether a character is a printed character, and its prototype is:

  int isprint (int c);

"Parameter" C is the character that needs to be detected.

' Return value ' If C is a printable character, returns a value other than 0, otherwise returns 0.

The ASCII value of printable characters is greater than 0x1f (except for 0x7f (DEL), these characters can be displayed on the screen, let us see, can not be displayed on the screen, we do not see, called control characters, ASCII code value of 0x00 ~ 0x1f, plus 0x7f (del). To detect control characters, use the Isiscntrl () function.

Note that this function is defined for a macro and is not a true function.

"Instance" determines which printable characters in the STR string contain space characters.

#include <ctype.h>
Main () {
  char str[] = "A5 @;";
  int i;
  for (i = 0; Str[i]!= 0. i++)
    if (Isprint (str[i)))
      printf ("str[%d" is printable character:%d\n ", I, Str[i]); 
   
    }

   

Output results:

Str[0] is printable character:a
str[1] are printable Character:5
str[2] is printable character:
str[3] is Prin Table character:@
str[4] is printable character:;

C language Isgraph () function: To determine whether a character is a printable character other than a space
header file:

#include <ctype.h>

Isgraph () is used to determine whether a character is a printable character other than a space, and its prototype is:

  int isgraph (int c);

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

Return value if C's ASCII code is printable and is not a space character, returns a value other than 0, or 0.

Note that isgraph () is a macro definition, not a true function.

"Example" determines which of the STR strings are printable characters.

#include <ctype.h>
Main () {
  char str[] = "A5 @;";
  int i;
  for (i = 0; Str[i]!= 0. i++)
    if (Isgraph (str[i)))
      printf ("str[%d" is printable character:%d\n ", I, Str[i]); 
   
    }

   

Output results:

Str[0] is printable character:a
str[1] are printable Character:5
str[3] is printable character:@ str[4
] is PRI ntable character:;

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.