C/C++函数库 之 ctype.h

#include < cctype>

一、官方解释

Character handling functions
This header declares a set of functions to classify and transform individual characters.

Functions
These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.

二、关于头文件

ctype定义了可以对单个字符进行分类和转换的相关函数。字符对应有相应的编码,函数的参数是字符参数的等价int编码,返回一个int值。返回值可以用于表示其他字符,也可以看做是bool值,0表示false,非0表示true。

Note:C语言中,用0表示“逻辑假”,用非0数表示“逻辑真”。C++中沿用了C的习惯,但增加了布尔类型变量(bool),布尔类型变量用true表示真,用false表示假。

三、库函数

int isalnum(int c);

1、作用:检查字符是否为字母数字。如果isalpha函数或isdigit函数也返回true,则结果为true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是字母、数字,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "ab2c.. -+";
	for(int i=0; i<9; i++)
	{
		if(isalnum(str[i]))
		{
			cout << "str[" << i << "] is alphabetic or number" << endl;
		}
	}
	
	return 0;
}

输出:
str[0] is alphabetic or number
str[1] is alphabetic or number
str[2] is alphabetic or number
str[3] is alphabetic or number

int isalpha(int c);

1、作用:检查字符是否为字母。如果是字母则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是字母,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "ab2c.. -+";
	for(int i=0; i<9; i++)
	{
		if(isalpha(str[i]))
		{
			cout << "str[" << i << "] is alphabetic" << endl;
		}
	}
	
	return 0;
}

输出:
str[0] is alphabetic
str[1] is alphabetic
str[3] is alphabetic

int isblank(int c);

1、作用:检查参数c是否为空格。如果是空格,则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是空,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "ab2c.. -+";
	for(int i=0; i<9; i++)
	{
		if(isblank(str[i]))
		{
			cout << "str[" << i << "] is blank" << endl;
		}
	}
	
	return 0;
}

输出:
str[6] is blank

int iscntrl(int c);

1、作用:检查参数c是否为控制符。如果是控制符,则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是控制符,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "ab2c..\n-+";
	for(int i=0; i<9; i++)
	{
		if(iscntrl(str[i]))
		{
			cout << "str[" << i << "] is control character" << endl;
		}
	}
	
	return 0;
}

输出:
str[6] is control character

int isdigit(int c);

1、作用:检查参数c是否为十进制数字。如果是十进制,则返回true
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是十进制数值,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str1 = "123ab";
	string str2 = "ab123";
	int num = 1;
	
	cout << isdigit(str1[0]);
	cout << isdigit(str2[0]);
	cout << isdigit(num);
	
	return 0;
}

输出:
1 0 0

int isgraph(int c);

1、作用:检查参数c是否有图形表示。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c有图形表示,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
图形化表示的字符是除了空格字符(’ ')之外的所有可以打印的字符(由isprint确定)。
3、实例

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

Note:借官方文档一用

int islower(int c);

1、作用:检查字符是否是小写字符字母
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是小写,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "123abABC";
	int i = 0;
	while(i<8){
		cout << islower(str[i++]) ;
	}
	
	return 0;
}

输出:(笔者用的DEV C++)
00022000

int isprint(int c);

1、作用:检查参数c是否是可以输出的
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是可以输出的,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
可打印字符是在显示中占据打印位置的字符。ASCII字符集中,可打印字符的ASCII码大于0x1f (US),小于0x7f (DEL)。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
    string str = "first line \n second line \n";
    int i=0;
    while(i<27)
    {
        if(isprint(str[i])){
            putchar(str[i]);
        }
        i++;
    }
    
    return 0;
}

输出:first line second line

int ispunct(int c);

1、作用:检查参数c是否是标点符号
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是标点符号,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
在c++中,该函数的特定于语言环境的模板版本(ispunct)存在于头文件locale.h中。

3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "Hello, world! Hello C";
	int cnt = 0;
	int i = 0;
	
	while(str[i])
	{
		if(ispunct(str[i++]))
			cnt++;
	}
	
	cout << cnt << " punctuation characters in total" << endl;
	
	return 0;
}

输出:2 punctuation characters in total

int isspace(int c);

1、作用:检查参数c是否是空白符
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是空白符,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
空白符包括:

字符ASCII含义
’ ’0x20空格(SPC)
’ \t ’0x09制表符(TAB)
’ \n ’0x0a换行(LF)
’ \v ’0x0b垂直制表符(VT)
’ \f ’0x0c换页符(FF)
’ \r ’0x0d回车(CR)

3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "Hello +\n World   hello C hello C++ ";
	int i = 0;
	while(str[i])
	{
		if(isspace(str[i]))
			putchar('1');
		else putchar('0');
		i++;
	}
	cout << '\n' << str << endl;
	
	return 0;
}

输出:
00000101100000111000001010000010001
Hello +
World hello C hello C++

int isupper(int c);

1、作用:检查参数c是否是大写字母
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是大写,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "123abABC";
	int i = 0;
	while(i<8){
		cout << isupper(str[i++]) ;
	}
	
	return 0;
}

输出:
00000111

int isxdigit(int c);

1、作用:检查参数c是否为十六进制数字
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是十六进制,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{
	string str = "16taf";
	int i = 0;
	
	while(str[i])
	{
		if(isxdigit(str[i]))
			putchar(str[i]);
		i++;
	}
	
	return 0;
}

输出:
16af
注:十六进制数是1、2、3、4、5、6、7、8、9、a、b、c、d、e、f

int tolower(int c);

1、作用:把大写字母转小写
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是大写字母,返回对应的小写字母
(3)参数:要转换的大写字母
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "Hello World 123";
	int i = 0;
	while(str[i])
	{
		putchar(tolower(str[i++]));
	}
	
	return 0;
}

输出:
hello world 123

int toupper(int c);

1、作用:把小写字母转大写
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是小写字母,返回对应的大写字母
(3)参数:要转换的小写字母
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{
	string str = "Hello World 123";
	int i = 0;
	while(str[i])
	{
		putchar(toupper(str[i++]));
	}
	
	return 0;
}

输出:
HELLO WORLD 123

  • 10
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值