Write a program that scrambles a sentence by: reversing the order of words in a
ID: 673408 • Letter: W
Question
Write a program that scrambles a sentence by: reversing the order of words in a sentence and shifting all the letters in each word by two positions to the right. When shifting to the right, the letters wrap around to the beginning. This program should: prompt a user to enter a sentence ending with either a period, a question mark or an exclamation mark output the same sentence with all of the words, with letters shifted to the right by two positions and printed in reverse order. Here is an example of how the program should behave:Explanation / Answer
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* scramble(char* str2)
{
int x = strlen(str2);
int k=0,ind=0;
int i;
char token[10][15];
char str1[80];
char *p=str1;
for(i=0;i<strlen(str2)-1;i++)
{
if(isspace(str2[i]))
{
printf(" %s",token[k]);
k++;
ind=0;
i++;
}
token[k][ind++]=str2[i];
}
for(i=0;i<k;i++)
{
token[i][strlen(token[i])-1]=token[i][strlen(token[i])-2];
strcat(str1,token[i]);
strcat(str1," ");
}
return p;
}
main()
{
char word[80];
printf("Enter a sentence:");
gets(word);
printf("Scrambled Sentence: %s",scramble(word));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.