微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决大写单个字母而不是整个句子

例如我如何大写字母“ 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]);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。