In: Computer Science
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:
More requirements:
More assumptions:
Sample Run:
Please enter the original sentence: i LOVE to program Translated: IKPU OVELKPU OTKPU ROGRAMPKPU
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
.