zoukankan      html  css  js  c++  java
  • STL <cctype>

    http://www.cplusplus.com/reference/cctype/

    isalnum

    View Code
    /* isalnum example */
    /*Checks whether c is either a decimal digit or an uppercase or lowercase letter.*/
    #include <stdio.h>
    #include <cctype>
    int main ()
    {
      int i;
      char str[]="c3Po...";
      i=0;
      while (isalnum(str[i])) i++;
      printf ("The first %d characters are alphanumeric.\n",i);
      return 0;
    }

    isalpha

    View Code
    /* isalpha example */
    #include <stdio.h>
    #include <cctype>
    int main ()
    {
      int i=0;
      char str[]="C++";
      while (str[i])
      {
        if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
        else printf ("character %c is not alphabetic\n",str[i]);
        i++;
      }
      return 0;
    }

    isdigit

    View Code
    /* isdigit example */
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main ()
    {
      char str[]="1776ad";
      int year, i=0;
      while(isdigit(str[i])) i++;
      printf("%d\n",i-1);
      if (isdigit(str[0]))
      {
        year = atoi (str);
        printf ("The year that followed %d was %d.\n",year,year+1);
      }
      return 0;
    }

    islower  and toupper

    View Code
    /* islower example */
    #include <stdio.h>
    #include <ctype.h>
    int main ()
    {
      int i=0;
      char str[]="Test String.\n";
      char c;
      while (str[i])
      {
        c=str[i];
        if (islower(c)) c=toupper(c);
        putchar (c);
        i++;
      }
      return 0;
    }

    ispunct

    View Code
    /* ispunct example */
    /*Checks whether c is a punctuation character*/
    #include <stdio.h>
    #include <cctype>
    int main ()
    {
      int i=0;
      int cx=0;
      char str[]="Hello, welcome!";
      while (str[i])
      {
        if (ispunct(str[i])) cx++;
        i++;
      }
      printf ("Sentence contains %d punctuation characters.\n", cx);
      return 0;
    }

    isupper and tolower

    View Code
    /* isupper example */
    #include <stdio.h>
    #include <cctype>
    int main ()
    {
      int i=0;
      char str[]="Test String.\n";
      char c;
      while (str[i])
      {
        c=str[i];
        if (isupper(c)) c=tolower(c);
        putchar (c);
        i++;
      }
      return 0;
    }
  • 相关阅读:
    什么是“方法”
    break与continue
    循环结构2
    循环结构1
    Switch多选择结构
    if选择结构
    Scanner方法
    Doc参数信息
    运算符号
    变量与常量
  • 原文地址:https://www.cnblogs.com/zhang1107/p/2971077.html
Copyright © 2011-2022 走看看