string对象中字符的处理

我们经常要对string对象中的单个字符进行处理,例如,通常需要知道某个特殊字符是否为空白字符、字母或数字.下面有不少各种字符操作函数,适用于string对象的字符(或其他任何char值).这些函数都在cctype头文件中定义 .(书上P77)

举一个例子.ispunct(c)函数的意思是,如果c是标点符号,则为true.

string s("Hello World!!!");
string::size_type punct_cnt=0;
//count number of punctuation characters in s
for(string::size_type index=0;index!=s.size();++index)
if(ispunct(s[index]))
++punct_cnt;
cout<<punct_cnt
    <<" punctuation characters in "<<s<<endl;
这个程序输出的结果是:

3 punctuation characters in Hello World!!!

这个程序从s这个字符串的左边开始扫描,扫到一个标点符号的时候,punct_cnt增1,扫到三个感叹号,因此出书punct_cnt为3.

和返回真值的函数不同的是,tolower和toupper函数返回的是字符,返回实参字符本身或返回该字符相应的大小写字符.我们可以用tolower函数把string对象s中的字母改为小写字母,程序如下:

//convert s to lowercase
for(string::size_type index=0;index!=s.size();++index)
s[index]=tolower(s[index]);
cout<<s<<endl;

得到的结果是:

hello world!!!

注:tolower(c)函数的意思是,如果c是大写字母,则返回其小写字母形式,否则直接返回c.

toupper(c)函数的意思是,如果c是小写字母,则返回其大写字母形式,否则直接返回c.

习题3.7:编写一个程序读入两个string对象,测试它们是否相等.若不相等,则指出两个中哪个较大.接着,改写程序测试它们的长度是否相等,若不相等指出哪个较长.

比较大小:

#include<iostream.h>
#include<string.h>
using namespace std;
int main()
{
 string s1,s2;
 //读入两个string对象
 cout<<"Enter two strings:"<<endl;
 cin>>s1>>s2;
 //测试两个string对象是否相等
 if(s1==s2)
  cout<<"They are equal."<<endl;
 else if(s1>s2)
  cout<<"/""<<s1<<"/"is bigger than"
      <<"/""<<s2<<"/""<<endl;
 else
  cout<<"/""<<s2<<"/"is bigger than"
      <<"/""<<s1<<"/""<<endl;
 return 0;
}

比较长度相等:

#include<iostream.h>
#include<string.h>
using namespace std;
int main()
{
 string s1,s2;
 //读入两个string对象
 cout<<"Enter two strings:"<<endl;
 cin>>s1>>s2;
 //比较两个string对象的长度
 string::size_type len1,len2;
 len1=s1.size();
 len2=s2.size();
 if(len1==len2)
  cout<<"They have same length."<<endl;
 else if(len1>len2)
  cout<<"/""<<s1<<"/"is longer than"
      <<"/""<<s2<<"/""<<endl;
 else
  cout<<"/""<<s2<<"/"is longer than"
      <<"/""<<s1<<"/""<<endl;
 return 0;
}

这里用了一个取字符串长度的函数s.size(),用了另外一个变量来记录,这个string::size_type跟int差不多,是专门用来记录这些字符串函数返回值的.前面说过.

习题3.8:编写一个程序,从标准输入读取多个string对象,把它们连接起来存放到一个更大的string对象中.并输出连接后的string对象.接着,改写程序,将连接后相邻string对象以空格隔开.

#include<iostream.h>
#include<string>
using namespace std;
int main()
{
 string result_str,str;
 //读入多个string对象并进行连接
 cout<<"Enter strings(Ctrl+Z to end):"<<endl;
 while(cin>>str)
  result_str=result_str+str;
 //输出连接后的string对象
 cout<<"String equal to the concatenation of these strings is:"
  <<endl<<result_str<<endl;
 return 0;
}

+=也是可以操作的.跟以前一些输出N个数字加起来差不多,不同的是定义的类型不同.

改写的程序:

#include<iostream.h>
#include<string>
using namespace std;
int main()
{
 string result_str,str;
 //读入多个string对象并进行连接
 cout<<"Enter strings(Ctrl+Z to end):"<<endl;
 cin>>result_str;//读入第一个string对象,放到结果对象中
 while(cin>>str)
  result_str=result_str+' '+str;
 //输出连接后的string对象
 cout<<"String equal to the concatenation of these strings is:"
  <<endl<<result_str<<endl;
 return 0;
}

先输入第一个字符串到结果变量,然后就在运算式上面加个空格...

习题3.9 下列程序实现什么功能?实现合法吗?如果不合法,说明理由

string s;
cout<<s[0]<<endl;

实现不合法,因为s是没有定义的空字符串,长度为0,输出没有的数据是非法的.s[0]无效.

在一些编译器例如微软的Visual C++,NET2003中,该程序段并不出现编译错误.

习题3.10 编写一个程序,从string对象中去掉标点符号.要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号后的string对象.

#include<iostream.h>
#include<string>
#include<cctype>
using namespace std;
int main()
{
 string s,result_str;
 bool has_punct=false;//用于标记字符串中有无标点
 char ch;
 //输入字符串
 cout<<"Enter a string:"<<endl;
 getline(cin,s);
 //处理字符串:去掉其中的标点
 for (string::size_type index=0;index!=s.size();++index)
 {
  ch=s[index];
  if(ispunct(ch))
   has_punct=true;
  else
   result_str+=ch;
 }
 if(has_punct)
  cout<<"Result:"<<endl<<result_str<<endl;
 else{
  cout<<"No punctuation character in the string?!"<<endl;
  return -1;
 }
 return 0;
}

这题我觉得比较难,它先是定义了两个字符串变量以及一个布尔型并初始化,还定义了一个char型.

getline(cin,s)这个函数是读取输入的字符串s,接下来似乎一个for循环,不断把字符串s里面的各个字符复制到char型变量ch中,然后用一个cctype头文件里面的ispunct函数来判断其是不是一个标点符号,是的话has_punct为真,如果不是标点符号则把它复制到定义的结果字符串变量result_str.做完循环之后的程序看不太懂 - -

 if(has_punct)
  cout<<"Result:"<<endl<<result_str<<endl;
 else{
  cout<<"No punctuation character in the string?!"<<endl;
  return -1;
 }

has_punct真就输出结果?可做完循环之后has_punct是什么?

先保存下点,慢慢想

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值