C++ 字符串首字母大写,其余小写转换 - 无需 toupper 函数
思路:将字符串首字母进行 ASCII 码转换,再将其转换为字符即可。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
if (s.length() > 0) {
s[0] = char(s[0] - 32);
}
for (int i = 1; i < s.length(); i++) {
s[i] = char(s[i] + 32);
}
cout << s << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oGvv 著作权归作者所有。请勿转载和采集!