How to replace a character at a specific index in a string with C++

2 Answers

0 votes
#include <iostream>
  
using namespace std;
   
int main() {
    string s = "cpp programming"; 
       
    s[4] = 'G';
    
    cout << s << endl;
}
  
  
   
/*
run:
   
cpp Grogramming
   
*/

 



answered Nov 13, 2019 by avibootz
0 votes
#include <iostream>
   
using namespace std;
    
int main() {
    string s = "cpp programming"; 
        
    s.replace(4, 1, "D"); 
     
    cout << s << endl;
}
   
   
    
/*
run:
    
cpp Drogramming
    
*/

 



answered Jan 10, 2020 by avibootz
...