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

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

  • ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which...

    ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which prompts the user for a string with 3 words each separated by a single space and reads the line into one String variable using nextline(). Extract each word and add them to a new String variable in reverse order with a space between the words. Have the 1st letter of the new string in upper case and all other strings in lower case letter....

  • With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads...

    With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads a text file “input.txt” and stores all the distinct words in an array. A word consists of letters only - uppercase and/or lowercase. An incoming word should be inserted into the array such that it is always in ascending order. Use binary search to ensure that no duplicate words are added. Assume that there are no more than 100 distinct words. Assume that the...

  • 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

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

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