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;
}

 

 

posted @ 2013-03-20 14:09  zhang1107  阅读(156)  评论(0编辑  收藏  举报