C++ 字符串首字母大写,其余小写转换 (无需 toupper)
思路:直接对第一个字符进行 ASCII 码加减运算即可。
C++ 代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
if (s.size() > 0) {
s[0] = toupper(s[0]);
for (int i = 1; i < s.size(); i++) {
s[i] = tolower(s[i]);
}
}
cout << s << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oGvw 著作权归作者所有。请勿转载和采集!