Capitalize a single letter instead of the entire sentence

68 views Asked by At

How do I uppercase for example letter "i"? I tried transform, convert. Now, I think using a for loop may be the best option, but cannot seem to figure it out!

#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;
}
2

There are 2 answers

0
Vlad Feinstein On BEST ANSWER

Here is an std:: way:

#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;
}
0
AudioBubble On
// 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]);
}