Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Write a program that reads in a line of text and replaces all four-letter wor

ID: 3733428 • Letter: 5

Question

5. 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! Programming Projects 501 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.

Explanation / Answer

#include <iostream>

#include <cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<istream>

using namespace std;

int main()
{
   char userString[101], ch1, ch2;
   int i, count;
   while (1) {
   i = 0; count = 0;
   cout << "Please enter a sentence to be modified: " << endl;
   cin.getline(userString, 100);
   while(userString[i] != ''){   
   if ( isalpha(userString[i]) ) {
   count++;
   } else {
   if (count == 4){
   if ((userString[i-4] >= 'A') && (userString[i-4] <= 'Z')) {
   userString[i-4] = 'L';
   } else {
   userString[i-4] = 'l';
   }
   userString[i-3] = 'o';
   userString[i-2] = 'v';
   userString[i-1] = 'e';
   }
   count = 0;
   }
   i++;
   }
   cout << userString << endl;
   cout << "Do you want to continue (Y/N) ? : " ;
   cin >> ch1;
   if (ch1 == 'N') {
   break;
   } else {
   cin.getline(userString, 100);
   }
   }
}

/************* Output of Program *****************
Please enter a sentence to be modified:
I hate you, you dodo!
I love you, you love!
Do you want to continue (Y/N) ? : Y
Please enter a sentence to be modified:
John will run home.
Love love run love.
Do you want to continue (Y/N) ? : N
*************************************************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote