下面的程序是对于给定的字符串,将其首字母变为大写。
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string toUpper(string &s)
{
string rst;
string firstChar=s.substr(0,1);
transform (firstChar.begin(), firstChar.end(), firstChar.begin(), (int(*)(int))toupper);
rst=firstChar+s.substr(1);
return rst;

}
int main(void)
{
string s;
cin>>s;
string rst=toUpper(s);
cout<<rst<<endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string toUpper(string &s)
{
string rst;
string firstChar=s.substr(0,1);
transform (firstChar.begin(), firstChar.end(), firstChar.begin(), (int(*)(int))toupper);
rst=firstChar+s.substr(1);
return rst;
}
int main(void)
{
string s;
cin>>s;
string rst=toUpper(s);
cout<<rst<<endl;
return 0;
}若变为小写,则需将 toupper 变为 tolower.
本文介绍了一个简单的C++程序,该程序能够接收一个字符串输入,并将字符串的首字母转换为大写。通过使用标准库函数transform配合toupper实现字符大小写的转换。
7669

被折叠的 条评论
为什么被折叠?



