#include<iostream>
#include<string>
using namespace std;
int main() {
string s1("hello,world!!!"); //测试了下不能为中文,暂未找到原因
decltype(s1.size()) x1 = 0; //使用decltype返回s.size的数据类型,x1的类型就是返回的数据类型,且初始化x1为0
//这里尝试使用auto来让编译器决定x1的数据类型,测试可用 (.size返回变量包含的字符个数.)
for (auto c:s1) //使用auto让编译器来决定变量c的数据类型
if (ispunct(c)) //如果c是标点符号
++x1;
cout << s1 << "的标点符号有: " << x1 << "个" << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1("hello,world!!!");
for (auto &c:s1) //遍历s1,使用的是引用c,而且这里的c必须初始化类型,这里用的auto,转换后等于是char
c=toupper(c); //for循环每次遍历都将把自己的值得替换为大写(toupper),间接改变了s1为大写
cout << s1 << endl;
return 0;
}
2 条评论
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1("helloworld");
int s2 = 0;
while (s2<10) {
s1[s2] = 'X';
++s2;
}
cout << s1 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
const string s1("0123456789ABCDEF"); //十六进制数
cout << "请输入一个数字,我将帮你把他转换为16进制(限0-15)" << endl;
string zancun;
decltype(s1.size()) n; //或者string::size_type n;
while (cin >> n) //该循环每次用户输入可以以空格隔开多个,但是必须用输入结束符ctrl+z才能正常cout
if ( n < s1.size())
zancun += s1[n];
cout << "对应的16进制数是: " <<zancun<< endl;
return 0;
}