C/ctype.h

字符处理函数

1.  isalnum 函数

int isalnum ( int c );

函数功能介绍:检查 是十进制数字还是大 、小 写字母。c 是字母活着数字则返回 true;注意,被认为是字母的内容可能取决于所使用的语言环境;在默认的“C”语言环境中,组成字母的是大写或小写返回 true 的值。

返回值:true返回非零值,false返回零。

/* isalnum example */
#include <stdio.h>
#include <ctype.h>
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;
}


output:The first 4 characters are alphanumeric.

2. isalpha函数

int isalpha ( int c );

功能介绍:检查 c 是否为字符字母。

注意,被认为是字母的内容取决于所使用的语言环境;在默认的“C”语言环境中,组成字母的只有大写或小写返回 true 的值。

使用其他区域设置时,字母字符是大写或小写返回true的字符,或者是区域设置明确认为是字母的另一个字符(在本例中,字符不能是iscntrl、数字、ispunct或空格)。

参数:要检查的字符,转换为 int 或 EOF。

返回:true返回非零值,false返回零值。

/* isalpha example */
#include <stdio.h>
#include <ctype.h>
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;
}

output:
character C is alphabetic
character + is not alphabetic
character + is not alphabetic

3.isblank 函数

int isblank ( int c );

函数介绍:检查 c 是否为空字符。空白字符是用于在一行文本中分隔单词的空格字符。标准的“C”语言环境考虑空白字符、制表符('\t')和空格字符(' ')。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果是空格;零,如果不是空格。

/* isblank example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  char c;
  int i=0;
  char str[]="Example sentence to test isblank\n";
  while (str[i])
  {
    c=str[i];
    if (isblank(c)) c='\n';
    putchar (c);
    i++;
  }
  return 0;
}

output:

Example
sentence
to
test
isblank

4.iscntrl 函数

int iscntrl ( int c );

函数介绍:检查 c 是否为控制字符。控件字符不占用显示中的打印位置(与可打印字符相反,用isprint检查)。

注意:对于标准ASCII字符集(由“C”语言环境使用),控制字符是ASCII码0x00 (NUL)和0x1f (US)之间的字符,加上0x7f (DEL)。

参数:要检查的字符,转换为int或EOF。

返回值:非零,是控制符;零,不是控制符。

/* iscntrl example */
#include <stdio.h>
#include <ctype.h>
#include<Windows.h>
int main()
{
	int i = 0;
	char str[] = "first line \n second line \n";
	while (!iscntrl(str[i]))
	{
		putchar(str[i]);
		i++;
	}
	printf("%s", str);
	system("pause");
	return 0;
}

output:

first line first line
 second line
请按任意键继续. . .

5.isdigit 函数

int isdigit ( int c );

函数介绍:检查c是否为十进制数字字符。小数位数是:0 1 2 3 4 5 6 7 8 9

参数:要检查的字符,转换为int或EOF。

返回值:非零,是数字;零,不是数字。

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

output:

The year that followed 1776 was 1777

6.isgraph 函数

int isgraph ( int c );

函数介绍:检查 c 是否为图形表示的字符。除了空格字符(' ')外,具有图形表示的字符都是可以打印出来的(由isprint确定)。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果c确实是一个表示字符。;零,如果c不是一个表示字符。

/* isgraph example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  FILE * pFile;
  int c;
  pFile=fopen ("myfile.txt","r");
  if (pFile)
  {
    do {
      c = fgetc (pFile);
      if (isgraph(c)) putchar (c);
    } while (c != EOF);
    fclose (pFile);
  }
}

7.islower 函数

int islower ( int c );

函数介绍:检查c是否为小写字母。

注意,被认为是字母的内容可能取决于所使用的语言环境;在默认的“C”语言环境中,小写字母是:a b c d e f g h i j k l m n o p q r s t u v w x y z。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果c确实是一个小写字符。;零,如果c不是一个小写字符。

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

output:

TEST STRING.

8.isprint 函数

int isprint ( int c );

函数介绍:检查c是否可打印字符。可打印字符是在显示器上占据打印位置的字符(与使用iscntrl检查的控制字符相反)。对于标准ASCII字符集(由“C”语言环境使用),打印字符的ASCII码都大于0x1f (US),只有0x7f (DEL)除外。

注意:isgraph在与isprint相同的情况下返回true,除了空格字符(' '),它在isprint检查时返回true,而在isgraph检查时返回false。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果 c 确实是一个可打印的字符;零,如果 c 不是一个可打印的字符。

/* isprint example */
#include <stdio.h>
#include <ctype.h>
#include<Windows.h>
int main()
{
	int i = 0;
	char str[] = "first line \n second line \n";
	while (isprint(str[i]))
	{
		putchar(str[i]);
		i++;
	}
	system("pause");
	return 0;
}

output:

first line 请按任意键继续. . .

9.ispunct 函数

int ispunct ( int c );

函数介绍:检查c是否为标点符号。

注意:C”语言环境将标点字符视为不是字母数字的所有图形字符(如isgraph中的字符)(如isalnum中的字符)。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果 c 确实是一个标点字符;零,如果 c 不是一个标点字符。

/* ispunct example */
#include <stdio.h>
#include <ctype.h>
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;
}

output:

Sentence contains 2 punctuation characters.

10.isspace 函数

int isspace ( int c );

函数介绍:检查c是否为空白字符。这些字符是

' ' (0x20) space (SPC)
'\t' (0x09) horizontal tab (TAB)
'\n' (0x0a) newline (LF)
'\v' (0x0b) vertical tab (VT)
'\f' (0x0c) feed (FF)
'\r' (0x0d) carriage return (CR)

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果 c 确实是一个空白字符;零,如果 c 不是一个空白字符。

/* isspace example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  char c;
  int i=0;
  char str[]="Example sentence to test isspace\n";
  while (str[i])
  {
    c=str[i];
    if (isspace(c)) c='\n';
    putchar (c);
    i++;
  }
  return 0;
}

output:

Example
sentence
to
test
isspace

11.isupper 函数

int isupper ( int c );

函数介绍:检查参数c是否为大写字母字母。

注意,被认为是字母的内容可能取决于所使用的语言环境;在默认的“C”语言环境中,大写字母是:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果 c 确实是一个大写字母;零,如果 c 不是一个大写字母。

/* isupper 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 (isupper(c)) c=tolower(c);
    putchar (c);
    i++;
  }
  return 0;
}

output:

test string.

12. isxdigit 函数

int isxdigit ( int c );

函数介绍:检查c是否为十六进制数字字符。十六进制数字是:0 1 2 3 4 6 7 8 9 a b c d e f A B C D E  F。

参数:要检查的字符,转换为int或EOF。

返回值:非零,如果 c 确实是一个十六进制数字字符;零,如果 c 不是一个十六进制数字字符。

/* isxdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="ffff";
  long int number;
  if (isxdigit(str[0]))
  {
    number = strtol (str,NULL,16);
    printf ("The hexadecimal number %lx is %ld.\n",number,number);
  }
  return 0;
}

output:

The hexadecimal number ffff is 65535.

 13. tolower 函数

int tolower ( int c );

函数介绍:如果c是大写字母且具有小写等效项,则将c转换为其小写等效项。如果不可能进行这种转换,则返回的值为c。

注意,被认为是字母的内容可能取决于所使用的语言环境;在默认的“C”语言环境,一个大写字母是:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z,分别翻译:a b c d e f g h i j k l m n o p q r s t u v w x y z。

参数:要检查的字符,转换为int或EOF。

返回值:如果C的小写存在,则返回输入c的小写值,否则返回输入值c。int值返回,可以隐式地转换为char。

/* tolower 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];
    putchar (tolower(c));
    i++;
  }
  return 0;
}

output:

test string.

14.toupper 函数

int toupper ( int c );

函数介绍:如果c是小写字母并且具有大写等效字母,则将c转换为其大写等效项。 如果不可能进行这样的转换,则返回的值不变。

请注意,所谓的字母可能取决于所使用的区域设置; 在默认的“C”语言环境中,小写字母是以下任意一个:a b c d e f g h i j k l m n o p q r s t u v w x y z,它们分别转换为:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

参数:要检查的字符,转换为int或EOF。

返回值:如果C的大写存在,则返回输入c的大写值,否则返回输入值c。int值返回,可以隐式地转换为char。

/* toupper 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];
    putchar (toupper(c));
    i++;
  }
  return 0;
}

output:

TEST STRING.

猜你喜欢

转载自blog.csdn.net/thecentry/article/details/82011648
今日推荐