zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    WIN7每次从关闭屏幕状态恢复都会挂断宽带连接,请问如何解决?
    程序設計学习之路:不走弯路,就是捷径
    Customize Firefox "Close tab" button
    域名常识
    一到十的英文单词,一十二个月份的英文单词,四季的英文单词,第一,第二第三的英文单词
    Dependency Walker
    刪除當前目錄隱藏文件,非隱藏文件,文件夾等好用的批處理。
    使用 Sandcastle Help File Builder 制作 VS.NET 的 HELP 文件
    字符“23.00”转成int型!Input string was not in a correct format.
    VisualStudio 2010 SP1安装时提示计算机环境导致无法安装的解决办法
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/7337808.html
Copyright © 2011-2022 走看看