vendredi 3 juillet 2015

Replacing and deleting a character from a string in c++?

This program is giving wrong output,, basically i want to remove the character specified and replace it by 'g'...For e.g: All that glitters is not gold if the user entered o then the output should be All that glitters is ngt ggld but the program is deleting all the characters from n onwards

 #include <iostream>
 using namespace std;
 int main()
 {
   string input(" ALL GLItters are not gold");
   char a;
   cin>>a;
   for(int i=0;i<input.size();i++)
   {
     if(input.at(i)==a)
     {
        input.erase(i,i+1);
        input.insert(i,"g");
     }
   }
    cout<<"\n";
   cout<<input;

 }

Aucun commentaire:

Enregistrer un commentaire