how can I fix this ?

Write a program that reads in a line of text and replaces all four-letter words
with the word “love”. For example, the input string
I hate you, you dodo!

should produce the output
I love you, you love!
Of course, the output will not always make sense. For example, the input
string
John will run home.
should produce the output
Love love run love.
If the four-letter word starts with a capital letter, it should be replaced by
"Love", not by "love". You need not check capitalization, except for the
first letter of a word. A word is any string consisting of the letters of the
alphabet and delimited at each end by a blank, the end of the line, or any
other character that is not a letter. Your program should repeat this action
until the user says to quit.


#include <iostream>
#include <string>


using namespace std;



int main()

{

string str1, str2;

int word_length = 0;




cout << " Hello! Enter a string:\n""\n";

getline(cin, str1);



str2 = str1;



int strL = str1.length();



for(int i = 0; i < strL; i++)

{

char item = str2[i];

if(item != ' ')

word_length++;

else if(item = ' ')

{

if(word_length == 4)

{

if(isupper(str2[i-4]))

str2[i-4] = 'L';

else

str2[i-4] = 'l';

str2[i-3] = 'o';

str2[i-2] = 'v';

str2[i-1] = 'e';

}
word_length = 0;

}

else if(item = '\n');

item = ' ';

}



cout << str2 << endl;



return 0;
}
With getline(cin, str1), the newline character '\n' isn't going to be saved in the string. You won't be able to check for the presence of that newline to know you're at the end of the string. You do know the length of the string and the current value of i.

The example in the problem description indicates that punctuation can be used, so I might use isalpha() to check if the character is a letter.

Check the equality operators - they should be == not =.
You'd also need to remove the punctuation from 4 letter words such as 'dodo!' in your example:
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
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <algorithm>

int main()
{
    std::cout << "Enter input string \n";
    std::string input{};
    getline(std::cin, input);

    std::istringstream stream{input};
    std::vector<std::string> words{};
    std::string single_word;
    while (stream >> single_word)
    {
        if(stream)words.push_back(std::move(single_word));
    }
    //http://stackoverflow.com/questions/236129/split-a-string-in-c

    std::cout << "words with punctuations \n";
    for (const auto& elem : words)std::cout << elem << " "; std::cout << "\n";
    for (auto& elem: words) elem.erase (std::remove_if (elem.begin (), elem.end (), ispunct), elem.end ());
    //http://stackoverflow.com/questions/19138983/c-remove-punctuation-from-string
    std::cout << "words w/o punctuations \n";
    for (const auto& elem : words)std::cout << elem << " "; std::cout << "\n";

    for (auto& elem : words)
    {
        if(elem.size() == 4)
        {
            elem = isupper(elem[0]) ?  "Love" : "love";
        }
    }

    std::cout << "with 4 letter words changed \n";
    for (const auto& elem : words)std::cout << elem << " "; std::cout << "\n";
}
You'd also need to remove the punctuation from 4 letter words such as 'dodo!' in your example:


? It looks like they're expected to keep the punctuation in the output.

I hate you, you dodo!
should produce the output
I love you, you love!

John will run home.
should produce the output
Love love run love.
indeed, you're right. how about this:
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
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>

int main()
{
    std::cout << "Enter input string \n";
    std::string input{};
    getline(std::cin, input);

    std::istringstream stream{input};
    std::vector<std::string> words{};
    std::string single_word;
    while (stream >> single_word)
    {
        if(stream)words.push_back(std::move(single_word));
    }
    //http://stackoverflow.com/questions/236129/split-a-string-in-c

    std::cout << "words from input string \n";
    for (const auto& elem : words)std::cout << elem << " "; std::cout << "\n";

    for (auto& elem : words)
    {
        if(elem.size() == 4 && !ispunct(elem.back()))
        {
            elem = isupper(elem[0]) ?  "Love" : "love";
        }
        if (elem.size() == 5 && ispunct(elem.back()))
        {
            std::string four_words = isupper(elem[0]) ?  "Love" : "love";
            elem = four_words + elem.back();
        }
    }

    std::cout << "with 4 letter words changed \n";
    for (const auto& elem : words)std::cout << elem << " "; std::cout << "\n";
}
Last edited on
The original code posted isn't that far off. The conditions in the for loop can be tweaked.

1
2
3
4
5
6
7
8
9
10
11
get the current character in the string
if it is an alpha character
  increment word length
  //check for special case where: 
  if word length is now 4 AND this is the last character in the string
    change the 4 characters of the word to love or Love (start at i-3 in this case, not i-4)
else if word length is 4 //(and we know the current character isn't alpha if we got here)
  change the 4 characters of the word to love or Love
  reset word length to 0
else //(we know the character isn't alpha and the word length is not 4)
  reset word length to 0
Last edited on
closed account (48T7M4Gy)
That sounds vert=y much like this:
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
#include <iostream>
#include <string>

int main(){
    std::string test = "I hate, lot's of words, Evil, NUTS and bolt?";
    std::cout << test << '\n';
    
    int count = 0;
    
    for(int i = 0; i < test.length(); ++i){
        
        if(isalpha(test[i])){
            if(count == 3 && !isalpha(test[i+1])){
                if(isupper(test[i-3]))
                    test[i-3] = 'L';
                else
                    test[i-3] = 'l';
                
                test[i-2] = 'o';
                test[i-1] = 'v';
                test[i] = 'e';
            }
            ++ count;
        }
        else
            count = 0;
    }
    
    std::cout << test << '\n';
    
    return 0;
}

I hate, lot's of words, Evil, NUTS and bolt?
I love, lot's of words, Love, Love and love?
Program ended with exit code: 0
Last edited on
@kledis23 - you'll also need to add in this requirement.

Your program should repeat this action
until the user says to quit.
Topic archived. No new replies allowed.