c++中ctype常用函数总结(isprint isblank..)

1 判断是否是二十六得字母中其中之一

   isalpha();

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <iostream>
 4 using namespace std;
 5 //是否是二十六个字母
 6 int main()
 7 {
 8     int i = 0;
 9     char str[] = "C++";
10     while (str[i])
11     {
12         if (isalpha(str[i])) printf("character %c is alphabetic
", str[i]);
13         else printf("character %c is not alphabetic
", str[i]);
14         i++;
15     }
16     std::cin.get();
17     return 0;
18 }
View Code

2 空白字符是用于在文本行内分隔单词的空格字符。

  isblank(int c)

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <iostream>
 4 using namespace std;
 5 //空白字符是用于在文本行内分隔单词的空格字符。
 6     int main()
 7     {
 8         char c;
 9         int i = 0;
10         char str[] = "what are you from
";
11         while (str[i])
12         {
13             c = str[i];
14             if (isblank(c)) c = '
';
15             putchar(c);
16             i++;
17         }
18         cin.get();
19         return 0;
20     
21 }
View Code

3 检查这个字符是否是控制字符

  int iscntrl(int c)

  (1) 一个控制字符是一个在显示上不占用打印位置的字符(这是一个可打印字符的反面,用isprint检查)

  (2)在标准c中ASCII的0x00-0x1f+0x7f

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 //遇到一个终止字符就停止输出
 7 int main()
 8 {
 9     int i = 0;
10     char str[] = "first apple 
 second apple 
";
11     while (!iscntrl(str[i]))
12     {
13         putchar(str[i]);
14         i++;
15     }
16     cin.get();
17     return 0;
18 }
View Code

4 检查这个字符是否是十进制数字字符

  int isdigit(int c)

  0-9

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <ctype.h>
 4 #include <iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     char str[] = "177667d";
 9     //int year = atoi(str);
10     int year;
11     if (isdigit(str[0]))
12     {
13         year = atoi(str);//只是会把数字部分留下
14         printf("The year that followed %d was %d.
", year, year + 1);
15     }
16     cin.get();
17     return 0;
18 }
View Code

5 检查字符是否是小写

int islower ( int c );
int toupper()转换为大写
 1 #include <stdio.h>
 2 #include <ctype.h>
 3 int main ()
 4 {
 5   int i=0;
 6   char str[]="Test String.
";
 7   char c;
 8   while (str[i])
 9   {
10     c=str[i];
11     if (islower(c)) c=toupper(c);
12     putchar (c);
13     i++;
14   }
15   return 0;
16 }
View Code

6 检查字符是否可以打印(屏幕显示)

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     int i = 0;
 8     char str[] = "first line 
 second line 
";
 9     while (isprint(str[i]))
10     {
11         putchar(str[i]);
12         i++;
13     }
14     cin.get();
15     return 0;
16 }
View Code

7 检查字符包含多少个标点符号

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     int i = 0;
 8     int cx = 0;
 9     char str[] = "Hello, welcome!";
10     while (str[i])
11     {
12         if (ispunct(str[i])) cx++;
13         i++;
14     }
15     printf("Sentence contains %d punctuation characters.
", cx);
16     cin.get();
17     return 0;
18 }
View Code

原文地址:https://www.cnblogs.com/lanjianhappy/p/7337808.html