Question

In: Computer Science

Q20. Using C++ style string to write a program that reads a sentence as input and...

Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below:

  • For every word in the sentence, the first letter is relocated the end of the word.
  • Then append the string “KPU” to the word.

More requirements:

  • All letters in the output should be uppercase.

More assumptions:

  • The input sentence contains no non-alphabetic letters

Sample Run:

Please enter the original sentence: i LOVE to program
Translated: IKPU OVELKPU OTKPU ROGRAMPKPU

Solutions

Expert Solution

Implement the program as follows:

  1. declare string variables
  2. ask the user to enter a sentence
  3. read complete sentence to sentence
  4. for each character in sentence, do the following,
  5. if character = ' ', it denotes the end of a word
  6. relocate first character to end, append "KPU" and append it to translated string
  7. set word = ""
  8. otherwise convert character to upper-case, cast to character and append it to word
  9. for the last word, relocate first character to end, append "KPU" and append it to translated string
  10. print translated string

Program:

#include <iostream>

using namespace std;

int main()
{
    string sentence, word = "", translated = "";                                /* declare string variables */
    cout<<"Please enter the original sentence: ";                               /* ask the user to enter a sentence */
    getline(cin, sentence);                                                     /* read complete sentence to sentence */
    for(int i=0;i<sentence.length();i++){                                       /* for each character in sentence */
        if(sentence[i] == ' '){                                                 /* if character = ' ', it denotes the end of a word */
            translated = translated + word.substr (1,string::npos)+ word[0] + "KPU ";   /* relocate first character to end, append "KPU" and append it to translated string */
            word = "";                                                          /* set word = "" */
        }
        else{                                                                   /* otherwise */
            word = word + (char)toupper(sentence[i]);                           /* convert character to upper-case, cast to character and append it to word */
        }
    }
    translated = translated + word.substr (1,string::npos)+ word[0] + "KPU ";   /* for the last word, relocate first character to end, append "KPU" and append it to translated string */
    cout<<"\nTranslated: "<<translated;                                         /* print translated string */
    
    return 0;
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT