How to convert part of a string to uppercase start from specific index in C++

1 Answer

0 votes
#include <iostream>

using namespace std;
 
string char_to_uppercase(string s, int idx) { 
    if (idx < 0 || idx > s.length()) return s;
            
    for (int i = 0; i < s.length(); i++) {
         if (i >= idx) {
             s[i] = toupper(s[i]);
         }
    }
    return s;
}
 
int main() {
    string s = "cpp programming"; 
     
    s = char_to_uppercase(s, 6);
     
    cout << s;
}


 
/*
run:
 
cpp prOGRAMMING
 
*/

 



answered Nov 13, 2019 by avibootz
...