Question

In: Computer Science

Write an interactive C program that asks a user to enter a sentence from the keyboard,...

Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards.

Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space.

Sample code execution: bold information entered by user

enter a sentence: hello ece 175 class

olleh ece 571 ssalc

continue (q to quit)? y

enter a sentence: May the force be with you

yaM eht ecrof eb htiw uoy

continue (q to quit)? a

enter a sentence: The quick brown fox jumps over a lazy dog

ehT kciuq nworb xof spmuj revo a yzal god

continue (q to quit)? d

enter a sentence: No lemon, no melon

oN ,nomel on nolem

continue (q to quit)? q

Solutions

Expert Solution

Below is the solution:

#include <stdio.h>
#include <string.h>
void reverse_string(char*);
void reverse_words(char*);

int main() {
int len;
while (1)
{//reading the data from console until we enter q to quit
printf("Enter a sentence: ");
char a[100];

fgets(a,sizeof(a),stdin);
len= strlen (a);
if (len>0 && a[len-1]=='\n')
a[--len]= '\0';
reverse_words(a);//reverses the words
printf("%s\n", a);
  
printf("continue (q to quit)?");
char quit[10];//takes the input if to quit
  
fgets(quit,sizeof(quit),stdin);
len= strlen (quit);
if (len>0 && quit[len-1]=='\n')
quit[--len]= '\0';
if (strcmp(quit,"q")==0){//if the entered char is q, then break the loop
break;
}
  
}
return 0;
}
//in this we split each word and pass it to reverse_strings to reverse each word
void reverse_words(char *s) {
char b[100], *t, *z;
int c = 0;

t = s;

while(*t) { //processing complete string
while(*t != ' ' && *t != '\0') { //extracting word from string
b[c] = *t;
t++;
c++;
}
b[c] = '\0';
c = 0;

reverse_string(b); // reverse the extracted word

z = b;

while (*z) { //copying the reversed word into original string
*s = *z;
z++;
s++;
}

while (*s == ' ') { // skipping space(s)
s++;
}
  
t = s; // pointing to next word
}
}

/*
* Function to reverse a word.
*/

void reverse_string(char *t) {
int l, c;
char *e, s;

l = strlen(t);
e = t + l - 1;

for (c = 0; c < l/2; c++) {
s = *t;
*t = *e;
*e = s;
t++;
e--;
}
}


Related Solutions

Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
In the space provided below write a ******C program********* that asks the user to enter their...
In the space provided below write a ******C program********* that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen. Using the embed icon shown above, also include screenshots demoing the execution of your program. Please write carefully. Thank you
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT