Question

In: Computer Science

SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...

SOLVE IN C: 6.31 LAB: Print string in reverse

Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.

Ex: If the input is:

Hello there
Hey
quit

then the output is:

ereht olleH
yeH

Hint: Use the fgets function to read a string with spaces from the user input. Recall that if a newline character is read from the user input before the specified number of characters are read, the newline character itself is also written into the string.

Solutions

Expert Solution

Screenshot of the code:

Sample Output:

Code to copy:

//Include the necessary header files

#include <stdio.h>

#include <string.h>

//Declare the maximum size as 50

#define MAX 50

//The main function

int main(void)

{

// Declare the array, names to store the input strings

char names[MAX];;

// Declare the 2-D array to store the characters of the input

//strings

char arr[10][50];

// Declare the required variables

int i, j, index, length;

// Initialize the index and length to 0.

index = length = 0;

while(1)

{

     // Take the strings as input in the array names such that the // maximum length does not exceed the value stored in MAX.

      fgets(names, MAX, stdin);

      //length is the variable that stores the length of

      //all the strings stored in names.

    length = strlen(names);

      //delete the trailing spaces.

      names[length - 1] = '\0';

      //If the user enters Quit/quit/q, then the break statement is // encountered.

      if(!strcmp(names,"quit") || !strcmp(names,"Quit") ||!strcmp(names,"q") )

      {

        break;

      }

     //Copy the strings stored in names to the array arr

      strcpy(arr[index], names);

    

     // Increment the index

      index++;

}

//Iterate the loop from 0 to index

for(i = 0; i < index; i++)

{

      //Iterate the strings in reverse manner, starting from the //end.

      for(j = strlen(arr[i]) - 1; j >= 0; j--)

      {

        //Print the reveresed strings

        printf("%c", arr[i][j]);

      }

    //Print the reversed strings in separate lines.

    printf("\n");

}

return 0;

}


Related Solutions

Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
write a simple c program to reverse pairs of characters in a string. For exaple, "notified"...
write a simple c program to reverse pairs of characters in a string. For exaple, "notified" becomes "edfitino". The reversal should be in this order in a simple easy to understand c 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.
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT