C language standard library (1)-# include (ctype. h)

Source: Internet
Author: User

C language standard library (1)-# include (ctype. h)
C Standard Library-# include By jxlijunhao in this header file contains a function for character testing, such as checking whether a character in a string is a number or a letter, and also contains a character ing function, for example, you can map uppercase letters to lowercase letters. The following describes the usage of some functions in the Standard Manual for future use.
1. isalpha: determines whether it is an isdigit letter or a number.

#include
 
  #include
  
   int main(){int i=0;char str[]="c++11";while (str[i]){if (isalpha(str[i]))printf("character %c is alpha\n",str[i]);else if (isdigit(str[i]))printf("character %c is digit\n",str[i]);else printf("character %c is not alpha or digit\n",str[i]);i++;}}
  
 
Output result:
character c is alphacharacter + is not alphacharacter + is not alphacharacter 1 is digitcharacter 1 is digit
Isxdigit: determines whether it is ~ F, ~ F


2. isalnum: it is a letter or number.
# Include
 
  
# Include
  
   
Int main () {int I = 0; int count = 0; char str [] = "c ++ 11 "; // count the number of letters or numbers in a string. while (str [I]) {if (isalnum (str [I]) count ++; I ++ ;} printf ("there are % d alpha or digit is % s", count, str); return 0 ;}
  
 
Output result:
3


3. islower, whether it is lower case, isupper, whether it is upper case tolower, convert it to lower case touuper to upper case
# Include
 
  
# Include
  
   
Int main () {char str [] = "I love ShangHai"; // converts lowercase letters in the string to uppercase letters int I = 0; char c; while (str [I]) {c = str [I]; if (islower (c) str [I] = toupper (c); I ++ ;} printf ("% s \ n", str );}
  
 
I LOVE SHANGHAI

4. isspace: determines whether a character is a space.
#include 
 
  #include 
  
   int main (){  char c;  int i=0;  char str[]="Example sentence to test isspace\n";  while (str[i])  {    c=str[i];    if (isspace(c)) c='\n';    putchar (c);    i++;  }  return 0;
  
 
Output:
Examplesentencetotestisspace


One application: Contains 0 ~ 9, ~ The string (hexadecimal) of F is converted to an integer:
# Include
 
  
# Include
  
   
# Include
   
    
Long atox (char * s) {char xdigs [] = "0123456789 ABCDEF"; long sum; while (isspace (* s) ++ s; for (sum = 0L; isxdigit (* s); ++ s) {int digit = strchr (xdigs, toupper (* s)-xdigs; // The address minus sum = sum * 16 + digit ;} return sum;} int main () {char * str = "21"; long l = atox (str); printf ("% d \ n", l ); return 0 ;}
   
  
 

The strchr function is contained. The prototype of a function is
const char * strchr ( const char * str, int character );
Returns the position of the character to be searched in the string for the first time. The returned value is a pointer to the character.


Related Article

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.