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

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.
Using Perl language: Write a program that reads a string from the standard input, and uses...
Using Perl language: Write a program that reads a string from the standard input, and uses a Perl regular expression to test whether the string looks like a valid credit card number. The card numbers of four major companies follow the following formats: Visa 13 or 16 digits, starting with 4. MasterCard 16 digits, starting with 51 through 55. Discover 16 digits, starting with 6011 or 65. American Express 15 digits, starting with 34 or 37. You should test your...
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
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.
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 Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT