Replace character in a string

#include <iostream>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;

string replace(string s, char c1, char c2){
    int l = s.length();
    for(int i = 0; i < l; i++){
        if(s[i] == c2)
            s[i] = c1;
    }
    return s;
}

int main(){
    string str = "OcianLabz";
    cout<<"Input String : "<<str<<endl;
    char c1 = 'e', c2 = 'i';
    cout<<"After Replace string : "<<replace(str,c1,c2);
    return 0;
}

    Leave a Reply

    Your email address will not be published.

    Need Help?