Home »
C++ Programs
C++ program to replace a word by another given word in the string using class
Submitted by Shubh Pachori, on September 01, 2022
Problem statement
Given a string, we have to replace a word by another given word using the class and object approach.
Example:
Input:
Enter String : his book is here.
Output:
Enter String replace : his
Enter String to replace : her
Edited String : her book is here.
C++ code to replace a word by another given word in the string using the class and object approach
#include <iostream>
#include <string>
using namespace std;
// create a class
class String {
// private data members
private:
string str;
string strrep;
string strtorep;
// public members functions
public:
// getString() function to input string
void getString() {
cout << "Enter String : ";
getline(cin, str);
}
// getSwap() function to input replace
// word and by which replace
void getSwap() {
cout << "Enter String replace : ";
cin >> strrep;
cout << "Enter String to replace : ";
cin >> strtorep;
}
// swapString() function to swap words
string swapString() {
// initializing int and string type variables to
// perform operations and manipulations
int index_1, index_2;
string temp_1;
string temp_2;
// for loop for traversing the whole string
for (index_1 = 0; str[index_1]; index_1++) {
// copying words to temp_1 one by one of the string
temp_1 = str.substr(index_1, strrep.length()), temp_2;
// if replacing word is equal to the word in temp_1
if (strrep == temp_1) {
// the copy replacing word to temp_2
temp_2 = strtorep;
// for loop to swap words
for (index_2 = 0; index_2 < strrep.length(); index_2++) {
// append word in temp_2 to the string
str[index_1 + index_2] = temp_2[index_2];
}
}
}
// transfering null
str[index_1] = 0;
// returning the edited string
return str;
}
};
int main() {
// create an object
String S;
// string type variable to store edited string
string str;
// calling getString() function to insert the string
S.getString();
// getSwap() function to insert the replacing
// and replaced words
S.getSwap();
// calling swapString() function to swap the
// words in the string
str = S.swapString();
cout << "Edited String : " << str;
return 0;
}
Output
Enter String: sum of numbers
Enter String replace: sum
Enter String to replace: mul
Edited String : mul of numbers
Explanation
In the above code, we have created a class String, three string type string data member str, strrep, and strtorep to store the string, word to replace and replacing word, and public member functions getString(), getSwap() and swapString()to store the string and word to replace and replacing word and to swap words in the string.
In the main() function, we are creating an object S of class String, reading the string using the getString() function, reading word to be replaced and replacing words using getSwap() function, and finally calling the swapString() member function to swap words in the string. The swapString() function contains the logic to swap words in the string and printing the result.
C++ Class and Object Programs (Set 2) »