#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"); }
|
|
来自: 木俊 > 《c primer》