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.
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 =.
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 thiscase, not i-4)
elseif 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