C++ Program to replace a character with a given character in a string [DEVCPP/GCC]

PROGRAM

//Program to replace a character with a given character in a string 

#include <iostream>

using namespace std;

int main()
{
        int i,len;
        string str;
        char s_char, r_char;

        cout<<"ENTER STRING: ";
        getline(cin,str);

        cout<<"\nENTER CHARACTER TO BE REPLACED: ";
        cin>>s_char;

        cout<<"\nENTER REPLACING CHARACTER: ";
        cin>>r_char;

        len=str.length();

        for(i=0 ; i<len ; i++)
        {
                if(str[i]==s_char)
                {
                         str[i]=r_char;
                }
        }

        cout<<"\nMODIFIED STRING: "<<str;

        return 0;
}

OUTPUT

C++ Program to replace a character with a given character in a string with output

Share this

Related Posts

FIND US ON FACEBOOK!