string 对象中字符的处理

cctype中的函数

 
isalnum(c)  如果C是字母或者数字,则为True
isalpha(c) 如果C是字母 则为true
iscntrl(c) 如果C是控制字符,则为true
isdigit(c) 如果C是数字,则为true
isgraph(c) 如果C不是空格,但可打印,则为true
islower(c) 如果C是小写字母,则为true
isprint(c) 如果C是可打印的字符,则true
ispunct(c) 如果C是标点符号,则true
isspace(c) 如果C是空白字符,则为true
isupper(c) 如果C是大写字母,则true
isxdigit(c) 如果C是十六进制数,则为true
tolower(c) 如果C大写字母,返回其小写字母形式,否则直接返回C
toupper(c) 如果C是小写字母,则返回其大写字母形式,否则直接返回C

例1 测试代码  ispunct(c) 查出字符串中标点的个数

例1
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 using std::string;
 5 int main()
 6 {
 7 
 8     string s("HELLO WORLD!!!");
 9     string::size_type cnt =0;
10                    // 定义个数
11     for(string::size_type index =0; index !=s.size();++index)
12         if(ispunct(s[index]))
13             ++cnt;
14         cout<<s<<"  "<<cnt<<endl;
15         
16      return 0;
17 
18 }

输出结果:

例2 测试代码  tolower(c) 返回其小写字母:

例2
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 using std::string;
 5 int main()
 6 {
 7 
 8     string s("HELLO WORLD!!!");
 9                 //convert s to lowercase
10     for(string::size_type index=0; index !=s.size();++index)
11         s[index] =tolower(s[index]);
12     cout<<s<<endl;
13      return 0;
14 
15 }

输出结果:

原文地址:https://www.cnblogs.com/canyuexingchen/p/2633103.html