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

问题描述

例如我如何大写字母“ i”?我尝试转换,转换。现在,我认为使用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]);
}