Question

Write a program that takes in a line of text as input, and outputs that line of text in reverse.

 4.24 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. 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

 the output is:

 ereht ollel

 yeH


image.png

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

import java.util.Scanner;

//class LabProgram
public class LabProgram
{
   //main function
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
      
       //array to store inputs
       String[] inputs = new String[100];
      
       //variable to store input string
       String input;
      
       int i = 0;
      
       //loop to read inputs
       while(true)
       {
           //reading input string
           input = sc.nextLine();
          
           //if input string is Quit or quit or q
           if(input.equals("Quit") || input.equals("quit") || input.equals("q"))
               //exits the loop
               break;
          
           //storing input string into array
           inputs[i] = input;
          
           //incrementing i
           i++;
       }
      
       //loop to reverse each string in the array and display the output
       for(int j = 0; j < i; j++)
       {
           //finding length of each string
           int length = inputs[j].length();
          
           //variable to store reverse of each string
           String reverse = "";
          
           //loop to reverse the string
           for(int k = length - 1; k >= 0; k--)
           {
               //reversing string
               reverse = reverse + inputs[j].charAt(k);
           }
           //printing reversed string
           System.out.print("\n" + reverse);
       }
   }
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
Write a program that takes in a line of text as input, and outputs that line of text in reverse.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. 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 quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • Write a program that takes in a line of text as input, and outputs that line...

    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 уен Hint: Use the fgets function to read a string with spaces from the user...

  • C++ Print string in reverse

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. 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 quitthen the output is:ereht olleH yeH

  • Write a Perl program that prompts a use to input a line of text. If the line of text mentions bot...

    Write a Perl program that prompts a use to input a line of text. If the line of text mentions both John and Smith, the program outputs “FOUND.” Otherwise, it outputs “NOT FOUND.” Write a Perl program that prompts a use to input a line of text. If the line of text has a word that is capitalized but not ALL capitalized, the program outputs “FOUND.” Otherwise, it outputs “NOT FOUND”. For example, if the user enters “Fred,” the program...

  • Write a Perl program that prompts a use to input a line of text. If the...

    Write a Perl program that prompts a use to input a line of text. If the line of text has a word that is capitalized but not ALL capitalized, the program outputs “FOUND.” Otherwise, it outputs “NOT FOUND”. For example, if the user enters “Fred,” the program outputs “FOUND.” But if the user enters “fred” or “FRED,” the program outputs “NOT FOUND.”

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.

     4.18 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Exx If the input is: 1995 the output is: yes Ex If the input is: 42,000 1995! the output is no Hint: Use a loop and the Character isDigitO function. 418.1: LAB: Checker for integer string

  • Write a basic ARM program that takes a string as input, then outputs a string that...

    Write a basic ARM program that takes a string as input, then outputs a string that replaces all the vowels in the input string with 'x'. Vowels: a, A, e, E, i, I, o, O, u, U. **I am using DS-5 Eclipse Community Workspace with ARM assembly language.** The program should give the following input prompt: "Input a string: " Then, the program should output the string with the vowel(s) replaced with x. The program should terminate upon printing the...

  • 5.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    5.14 LAB: Convert to binary Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the...

  • Write a program that takes a date as input and outputs the date's season. The input...

    Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT