Question

Write a Java program that reads in 5 words from the user and stores these words...

Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information:

  • All words are in reverse order (based both on content and order).
  • Every word at an even index of the array (i.e., indices 0, 2, and 4).

For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two lines:

EW, KJ, YT, FED, CBA
ABC, TY, WE
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class TestCode {

    public static String reverse(String str){
        if(str.length() == 0){
            return "";
        }
        else{
            return str.charAt(str.length()-1)+reverse(str.substring(0,str.length()-1));
        }
    }

    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);

        String[] arr = new String[5];

        System.out.println("Enter 5 words");
        for(int i = 0;i<arr.length;i++){
            arr[i] = scanner.next();
        }

        for(int i = arr.length-1;i>=0;i--){
            System.out.print(reverse(arr[i])+" ");
        }
        System.out.println();

        for(int i = 0;i<arr.length;i++){
            if(i%2==0){
                System.out.print(arr[i]+" ");
            }
        }
        System.out.println();

    }
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that reads in 5 words from the user and stores these words...
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
  • Q1. CLO: (5 points) Write a java program that reads 10 words from the keyboard then...

    Q1. CLO: (5 points) Write a java program that reads 10 words from the keyboard then place them in an array. Display the array elements in reverse order, and then count and print the number of times a word appears in that array. The user shall insert the word from the keyboard.

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

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

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Write a Java program that first reads a positive integer from the user - let's call...

    Write a Java program that first reads a positive integer from the user - let's call it howMany. Then the program reads howMany pairs of integers - let's call them n1 and n2. For each pair, the program determines if the first component in the pair is a multiple of the second component, in other words, if n1 is a multiple of n2. For example, if numberOfPair is 5, your program will read 5 pairs of integers, and for each...

  • Write a C program that … reads (from standard input) five words, with each of the...

    Write a C program that … reads (from standard input) five words, with each of the words being entered on a separate line prints (to standard output) the words in reverse alphabetical order Hints http://www.cplusplus.com/reference/cstring/strcmp

  • Write a C++ program that reads in 6 elements from the keyboard and stores them in...

    Write a C++ program that reads in 6 elements from the keyboard and stores them in an array x[6]. It should then create a second array y[6] that contains the elements from x in reverse order and with the s Tsecond - Tsmallest shown below. A sample run should look like: rocted ITrom it (e.g., Jlast first Tsmallest Vsecond last , etc). The program should finally print out both arrays to screen, in the format Enter 6 array elements: 4...

  • Write a java program that reads a file (names as textfile) uploaded with assignment and displays...

    Write a java program that reads a file (names as textfile) uploaded with assignment and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

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