分享

处理string对象中的字符c++primer 81

 木俊 2018-08-04
#include<iostream>
using namespace std;
#include<string>
int main()
{
 //处理每个字符,范围for
string s1 = "aigjiouarjiu";
for (auto c : s1)
{
int i = 1;
cout<<"s1字符串的第"<<i<<"个字符:"<<c<<endl;
}

string s2 = "sg,gg.gga,,,,!!!!rgq/q/gq'qgr???";
decltype (s2.size()) punt=0;  //decltype (s1.size(),返回一个数据类型
for (auto c : s2)
{
if (ispunct(c))
{
punt++;
}
}
cout << "s2中有" << punt << "个标点符号" << endl;

//使用范围for改变字符串的值,声明部分为引用
string s3 = "huang jun mu";
for (auto &c : s3)
{
c = toupper(c);
}
cout << "s3:" << s3 << endl;
//只处理一部分字符
//下标运算符
string s4 = "huang junmu";
if (!s4.empty())
{
s4[0] = toupper(s4[0]);
}
cout << "s4: " << s4 << endl;
//使用下标进行迭代
for (decltype(s4.size()) index = 0; index != s4.size() && !isspace(s4[index]); ++index)
{
s4[index] = toupper(s4[index]);
}
cout << "s4: " << s4 << endl;


//使用下标执行随机访问
system("pause");
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约