Replacing a word in a text using loop

Hello,

How do you replace every "dicktionary" word in a text with "dictionary" using loop?

Original text: I love my dicktionary. A dicktionary helps me to improve my vocabs.
Corrected text: I love my dictionary. A dictionary helps me to improve my vocabs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;

void replaceword (string s1){
    do{
        
        s1.replace(0,11,"dictionary");

    }while(s1.find("dicktionary"));

    cout << s1 <<endl;

}

int main()
{

    string s1 ("I love my dicktionary. A dicktionary helps me to improve my vocabs.");
    cout << s1 << endl;
    replaceword(s1);

    cout << s1 << endl;

    return 0;
}


Thanks
Last edited on
[tt]std::replace or std::replace_if might be what you are looking for, found in <algorithm>.
https://en.cppreference.com/w/cpp/algorithm/replace[/tt]

Ooops, I totally misunderstood the question.
Last edited on
@mermaidly, @Furry Guy,

did you bother to look at the manual? Replace doesn't replace substrings in a string

https://en.cppreference.com/w/cpp/algorithm/replace
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

std::string ReplaceString(const std::string& src, const std::string& old, const std::string& replace)
  {
    std::size_t pos;
    std::string s(src);
    
    while((pos = s.find(old)) != std::string::npos)
    {
      s.replace(pos, old.size(), replace);  
    }
    return s;
  }

int main()
{
  std::string input("I love my dicktionary. A dicktionary helps me to improve my vocabs.");
  std::string output = ReplaceString(input, "dicktionary", "dictionary");
  
  std::cout << "output = " << output << '\n';
}

Output
output = I love my dictionary. A dictionary helps me to improve my vocabs.
Yes, Thomas, I did read the link. I gave it, originally. I misunderstood the question.
It's ok Furry Guy. To err is human.
Sorry if I sounded a bit harsh.
The question does ask about replacing words, rather than (sub)strings. So does this test case work?

"The C++ language has types int, lang, and short. It's not slang"

Where I just want to correct my spelling of the well-known integral type!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

std::string ReplaceString(const std::string& src, const std::string& old, const std::string& replace)
  {
    std::size_t pos;
    std::string s(src);
    
    while((pos = s.find(old)) != std::string::npos)
    {
      s.replace(pos, old.size(), replace);  
    }
    return s;
  }

int main()
{
  std::string input("The C++ language has types int, lang, and short. It's not slang!");
  std::string output = ReplaceString(input, "lang", "long");
  
  std::cout << "output = " << output << '\n';
}


Output:
output = The C++ longuage has types int, long, and short. It's not slong!


Whereas this approach works on a word-by-word basis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

// replaces words in string
// - implementation was restricted to well-known members of std::string. Code can be reduced
//   by using overloads of compare() and assign() members, etc.
// - this function is just an exercise. In "real life" could achieve the same effect using
//   std::regex_replace() Or could use some sort of iterator/algorithm-based approach
std::string ReplaceWord(const std::string& src, const std::string& old, const std::string& replace)
{
    std::string dest;

    const char non_alnum[]{ " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~" };

    std::size_t pos_from = 0;
    std::size_t pos_to = src.npos;
    while (pos_from != src.npos)
    {
        pos_to = src.find_first_not_of(non_alnum, pos_from);
        if (pos_to != src.npos)
        {
            std::string non_word = src.substr(pos_from, pos_to - pos_from);
            dest += non_word;
            pos_from = pos_to;

            pos_to = src.find_first_of(non_alnum, pos_from);
            size_t word_len = (pos_to == src.npos) ? src.npos : (pos_to - pos_from);
            std::string word = src.substr(pos_from, word_len);
            dest += (word == old) ? replace : word;
            pos_from = pos_to;    
        }
        else
        {
            std::string non_word = src.substr(pos_from);
            dest += non_word;
            pos_from = src.npos;
        }
    }
    return dest;
}

int main()
{
  std::string input("The C++ language has types int, lang, and short. It's not slang!");
  std::string output = ReplaceWord(input, "lang", "long");
  
  std::cout << "output = " << output << '\n';
}


Output:
output = The C++ language has types int, long, and short. It's not slang!

Andy
Last edited on
One does not need to search the already processed text repeatedly:
1
2
3
4
    std::size_t pos = 0;
    std::string s(src);
    
    while ( (pos = s.find(old, pos)) != std::string::npos )
Topic archived. No new replies allowed.