将单个字母大写,而不是整个句子

  • 本文关键字:句子 单个字 c++ toupper
  • 更新时间 :
  • 英文 :


我如何大写例如字母"我"?我试着转换,转换。现在,我认为使用for循环可能是最好的选择,但似乎无法解决!

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "this is a test, this is also a test, this is yet another test";
int strLen = 0;
strLen = s.length();
for() //this is where I can't seem to figure it out
cout << s << endl;
return 0;
}

这里有一种std::方式:

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
string s = "this is a test, this is also a test, this is yet another test";
char ch = std::toupper('i');
std::replace(s.begin(), s.end(), 'i', ch);
cout << s << endl;
return 0;
}
// Assume that all dependencies have been included
void replaceAllLetters (string& s, char toReplace) {
char new = std::toupper(toReplace);
std::replace(s.begin, s.end, toReplace, new);
}
void replaceOneLetter (string& s, int index) {
if (index < s.size()) s[index] = std::toupper(s[index]);
}

相关内容

  • 没有找到相关文章

最新更新