Find and replace a word in a given sentence or string in C++

In this C++ tutorial, we are going to discuss how to find all the occurrences of a word in a given sentence or string and replace it with a particular word. This feature can be seen in applications like Notepad, WordPad, VS code etc. where we find a word and replace all the occurrences of that word with a particular word.

How to read a sentence in C++

If we want to take user input then, we have to use getline() because if we use cin, then it will only take input up to a whitespace whereas getline() will take the entire input which is why it is often used for strings.

Example:

#include <string>
#include <iostream>
using namespace std;
int main()
{
string input;
cout<<"Enter a sentence: \n";
getline(cin, input);
cout << input;
return 0;
}

The code has been updated and working fine now.

C++ code to find and replace

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::string inputString, findWord, replaceWord;

    inputString = "Which is the best site to learn programming concepts.";

    findWord = "Which";

    replaceWord = "CodeSpeedy";

    std::cout << "Original string: \n"
              << inputString << std::endl;
    size_t pos = 0;
    while ((pos = inputString.find(findWord, pos)) != std::string::npos)
    {
        inputString.replace(pos, findWord.length(), replaceWord);
        pos += replaceWord.length();
    }

    std::cout << "Modified string: \n"
              << inputString << std::endl;

    return 0;
}

Explanation

  • The code starts by declaring a string inputString, and two variables findWord and replaceWord.
  • Then we initialize all 3 variables.
  • Then comes a while loop where pos starts at 0. It goes through all of inputString until it finds findWord or reaches npos (which stands for not found).
  • If pos does reach npos then it means that there was no match found so instead of continuing down into the loop, it just replaces everything up until findWord’s length position with replaceWord.
  • Then we print the modified string as output.

OUTPUT:

Original string: 
Which is the best site to learn programming concepts.
Modified string: 
CodeSpeedy is the best site to learn programming concepts.

Thank you for learning from CodeSpeedy.

If you have any doubts then feel free to ask them in the comments section below.

2 responses to “Find and replace a word in a given sentence or string in C++”

  1. Sarah says:

    This code doesn’t work. If I write any setence but I tried with your example like Which site should i prefer for learning programming concepts. Which site is the best site for day to day programming blogs.
    The code stops working and write:
    Enter the word to be replaced
    Enter the word to which it should be replaced
    Sentence before replacing:
    Which site should i prefer for learning programming concepts. Which site is the best site for day to day programming blogs.

    It doesn’t ask about the replaced word and the new word.

  2. Saruque Ahamed Mollick says:

    The post is updated with the correct code.

Leave a Reply

Your email address will not be published. Required fields are marked *