cctype头文件中的部分函数:
测试如下:
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main(int argc, char* argv[])
- {
- string a("hello word ! 123 AA0x34");
- unsigned char b = 1;
- string::size_type i;
- for ( i=0; i<a.size(); ++i)
- {
- if(isalnum(a[i])) // 如果a[i] 是字母或者数字,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (isalpha(a[i])) // 如果a[i] 是字母,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )//当a[i]在0x00-0x1F之间或等于0x7F(DEL)时,返回非零值,否则返回零。
- {
- if (iscntrl(a[i])) // 如果a[i] 是控制字符,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (isdigit(a[i])) // 如果a[i] 是数字,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (isgraph(a[i])) // 如果a[i] 是不是空格可打印的,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (ispunct(a[i])) // 如果a[i] 是标点符号,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (isspace(a[i])) // 如果a[i] 是空白,则返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- if (isxdigit(b))
- {
- cout<<'b ='<<b<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- if (isupper(a[i])) // 如果a[i] 大写字母,返回true
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- a[i] = (toupper(a[i])); // 如果a[i] 是小写字母,返回其大写字母,否则返回a[i]
- cout<< i <<"-----"<<a[i]<<endl;
- }
- cout<<"=========================="<<endl;
- for ( i=0; i<a.size(); ++i )
- {
- a[i] = (tolower(a[i])); // 如果a[i] 是小写字母,返回其大写字母,否则返回a[i]
- cout<< i <<"-----"<<a[i]<<endl;
- }
- return 0;
- }