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

Code

#include<iostream>
using namespace std;

int main()
{
    char s[100]; //input string
    gets(s); //get input
    char u[100]; //for each word
    int j=0;
    char t[100][100];
    int l=0;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]!=' ')
        {
            u[j++]=toupper(s[i]); //store elements in u till space character occurs
        }
        else //if space char occured do the changes like add "KPU" and relocate
        {
            char c=u[j-1];
            u[j-1]=u[0];
            for(int k=0;k<j-2;k++)
            {
                u[k]=u[k+1];
            }
            u[j-2]=c;
            u[j++]='K';
            u[j++]='P';
            u[j++]='U';
            strcpy(t[l++],u);
           
            for(int p=1;p<j+3;p++) //empty the word
            {
                u[p]='\0';
            }
            j=0;
        }
        //wothout this line last word will not be printed
        if(i==strlen(s)-1 && s[strlen(s)-1]!=' ') //last word need to check alone
        {
            char c=u[j-1];
            u[j-1]=u[0];
            for(int k=0;k<j-2;k++)
            {
                u[k]=u[k+1];
            }
            u[j-2]=c;
            u[j++]='K';
            u[j++]='P';
            u[j++]='U';
            strcpy(t[l++],u);
            j=0;
        }
    }
    for(int i=0;i<l;i++) //print all words
    {
        cout<<t[i]<<' ';
    }
    return 0;
}

Terminal Work

.


Related Solutions

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 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...
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++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. Ex: If the input is: 3 automobile car manufacturer maker children kids 15 The automobile manufacturer recommends car seats for children...
use C++ Write a program to calculate the frequency of every letter in an input string...
use C++ Write a program to calculate the frequency of every letter in an input string s. Your program should be able to input a string. Calculate the frequency of each element in the string and store the results Your program should be able to print each letter and its respective frequency. Identify the letter with the highest frequency in your message. Your message should be constructed from at least 50 letters. Example of Output: letters frequency a 10 b...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT