SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Strings
Exercise:
Programming Projects
Question:4 | ISBN:9780132846813 | Edition: 5

Question

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 following 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 following output:

Love love run love.

If the four-letter word starts wit h 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>  
#include <string>  
#include <cmath>  
#include <cctype> 
 
 
using namespace std; 
const int SIZE = 40; 
void get_input(string& input_line); 
 
 
string replace_with_love(string input_line); 
 
 
int main() 
{ 
start: 
string input_line, love_string; 
 
 
  get_input(input_line); 
  love_string = replace_with_love(input_line); 
 
 
  cout << love_string << endl; 
  system("pause"); 
  goto start; 
 
 
} 
 
 
void get_input(string& input_line) 
{ 
char answer; 
string one[SIZE]; 
int i = 0; 
string word; 
 
 
 cout << "Enter a line of text and press return (alternatevely, input a single character and press enter to end the program)" << endl; 
 
 
 getline(cin, input_line); 
} 
 
 
string replace_with_love(string input_line) 
{ 
string one[SIZE]; 
string love_string(input_line); 
string temp; 
unsigned int location, location2, count = 0, Love = 0; 
unsigned int pos = 0, last = 0; 
 
 
 if (love_string.length() <= 1) 
{ 
  system("pause"); 
  exit(1); 
} 
 
 
 last = input_line.find_last_of(" "); 
while ((pos < input_line.length()) && (count < SIZE)) 
{ 
  location = input_line.find(" ", pos); 
  location2 = input_line.find(" ", location + 1); 
 
 
  if (pos == 0) 
  { 
   one[count] = input_line.substr(pos, location); 
   count++; 
   pos = location; 
   continue; 
  } 
 
 
  if (last == location) 
  { 
   one[count] = input_line.substr((pos + 1), (location)); 
   one[count + 1] = input_line.substr(pos); 
   pos *= 10; 
  } 
 
 
 
  else if ((location > (last + 10)) || (location2 > (last + 10))) 
   pos *= 10; 
  else 
  { 
   one[count] = input_line.substr(pos + 1, ((location2)-(location + 1))); 
 
 
  } 
  pos = location2; 
  count++; 
 
 
 } 
 
 
 for (int i = 0; i < count; i++) 
{ 
  if (one[i].length() == 4) 
  { 
   temp = one[i]; 
   if (isupper(temp.at(0))) 
   { 
    one[i] = "Love"; 
    Love++; 
   } 
   else 
   { 
    one[i] = "love"; 
   Love++; 
   } 
  } 
} 
  if (Love < 2) 
  { 
   cout << "There are not enough 4 letter words in this sentence!" << endl; 
   system("pause"); 
   main(); 
  } 
  else { 
 
 
   love_string.clear(); 
   for (int n = 0; n < count; n++) 
    love_string += one[n] + " "; 
   return love_string; 
  } 
} 

 

0 0

Discussions

Post the discussion to improve the above solution.