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 a 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 <sstream>
#include <string>
using namespace std;
int main(){
string text;
//reads in a line of text
cout<<"Enter text: ";
getline(cin,text);
string word;
stringstream iss(text);
cout<<"\n";
// Read and print each word.
while (iss >> word){
string deletedSymbol="";
//check if word contains only letters
for (int i = 0; i < word.size(); i++) {
if (word[i] < 'A' || word[i] > 'Z' &&
word[i] < 'a' || word[i] > 'z'){
//delete symbol
deletedSymbol=word[i];
word.erase(i, 1);
}
}
//cout << word<<deletedSymbol<<" ";
if(word.length()==4){
//check if the first letter of the word is a capital letter
if(isupper(word[0])){
cout <<"Love";
}else{
cout <<"love";
}
}else{
cout << word;
}
//display deleted symbol again
cout<<deletedSymbol<<" ";
}
cout<<"\n\n";
return 0;
}
Comments
Leave a comment