from "i hate you" to "i love you"

every four-letter word is supposed to be replaced by the word love. unfortunately, only the first four-letter word of my input gets changed.

for example, my input would be:
i hate you fred
my output:
i love you fred

the correct output should be
i love you love

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
49
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <cctype>

using namespace std;

int main()
{

string text;
char again = 'y';
int letterOne = 0, letterTwo = 0;

while (again == 'y')
{
cout << "Enter a line of text.\n";
getline(cin, text);

    for(int i = letterOne; i < text.size(); i++)
    {
            if(text[i] == ' ' && text[i] != ',')
            {
            letterTwo = i;
            if((letterTwo - letterOne) == 5)
            {
            text.replace(letterOne + 1, 4, "love");

            if (letterOne == 0)
            {
            text.replace(letterOne, 4, "Love");
            }

            if (text[i] == '\0')
            text.replace(letterOne - 1, 4, "love");
            }
            letterOne = i;
            }
    }

cout << text << endl;

cout << "Would you like to try again? (y/n): ";
cin >> again;
}

     system("PAUSE");
     return 0;
}
If you are using strings, you could use .find()/.substr() and try to find a space then four letters then another space (unless it is the first word or last word). You could try iterating through all 6 letter substrings (5 for the first/last) and look for spaces on the ends, then use .replace().
I think, you should have a close look on your if-else statements. For example, what does this mean?

if(text[i] == ' ' && text[i] != ',')

Also rethink on wich position the integers letterOne and letterTwo are. I would say line 37 has to be

letterOne=i+1

to be on the first letter of the new word. And with this the condition in line 25 is

(letterTwo - letterOne) == 4

And in the end, your attempt of re-running the program doesn't work, because cin leaves a newline in the buffer. After cin you have got to type something like
cin.ignore(1000, '\n');
Topic archived. No new replies allowed.