c++toupper

Capitalize a single letter instead of the entire sentence


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

Solution

  • 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;
    }