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.
Are you trying to replace only the fourth word with 'love', or every four letter word with 'love'?
Right now it looks like you're changing every four letter word to 'love', except if the last word in the string entered is four letters. I added in a couple of cout statements to the code to print the interim values below.
Hello! Enter a string:
hate hate
item is h
word_length is 1
item is a
word_length is 2
item is t
word_length is 3
item is e
word_length is 4
item is
word_length is 0
item is h
word_length is 1
item is a
word_length is 2
item is t
word_length is 3
item is e
word_length is 4
love hate
In a couple of else ifs I think you want the equality operator == instead of the assignment operator.
The following program utilizes the fact that if a string were to have a fourth word it'd be bounded by the third occurrence of ' ' on the left and any one of the following characters on the right “ !?;.” (add other characters if required):
OP: above program should be generalized a bit – suppose you've read an entire file into a std::string. Now also suppose the first paragraph of this file has just three words but the file (and hence the string) has more than four words. In this case the above program would not work as the fourth word is left bounded by the newline character, not ' '.
So, what we need to do here would be to find the third instance of isspace() (#include cctype) within the string, and not just the char ' ', which would also account for the newline character in this case. http://en.cppreference.com/w/cpp/string/byte/isspace
Accordingly, the nthOccurrence() function from the above post has been updated:
// return start and end positions of next word at or after position 'from'
// return pair { str.size(), str.size() } if there is no next word
// word: longest sequence consisting solely of of alphanumeric characters
std::pair< std::size_t, std::size_t > next_word( const std::string& str, std::size_t from = 0 )
{
// skip over leading non-alphanumeric character
while( from < str.size() && !std::isalnum( str[from] ) ) ++from ;
std::size_t end = from ;
// skip to first trailing non-alphanumeric character
while( end < str.size() && std::isalnum( str[end] ) ) ++end ;
return { from, end } ;
}
// return start and end positions of nth word (n is zero based)
// return pair { str.size(), str.size() } if there is no nth word
std::pair< std::size_t, std::size_t > nth_word( const std::string& str, std::size_t n )
{
auto pair = next_word(str) ;
for( ; n > 0 ; --n ) pair = next_word( str, pair.second ) ;
return pair ;
}