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\n", str[i]);
13         else printf("character %c is not alphabetic\n", 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\n";
11         while (str[i])
12         {
13             c = str[i];
14             if (isblank(c)) c = '\n';
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 \n second apple \n";
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.\n", 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.\n";
 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 \n second line \n";
 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.\n", cx);
16     cin.get();
17     return 0;
18 }
View Code

 

转载于:https://www.cnblogs.com/lanjianhappy/p/7337808.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值