Write a program that initializes a string with Mississippi Then replace all i with ii and print the length of the resulting string. In that string replace all ss with s and print the length of the resulting string.

Respuesta :

Answer:

Explanation:

#include <bits/stdc++.h>  

using namespace std;  

string replace(string s, char c1, char c2)  

{  

   int l = s.length();  

 

   // loop to traverse in the string  

   for (int i = 0; i < l; i++) {  

 

       // check for c1 and replace  

       if (s[i] == c1)  

           s[i] = c2;  

 

       // check for c2 and replace  

       else if (s[i] == c2)  

           s[i] = c1;  

   }  

   return s;  

}  

 

// Driver code to check the above function  

int main()  

{  

   string s = "Mississippi";  

   char c1 = 'i', c2 = 's';  

   cout << replace(s, c1, c2);  

   return 0;  

}