Question

Problem: Read in a word and display the requested characters from that word in the requested...

Problem: Read in a word and display the requested characters from that word in the requested format. Write a Java program that

● prompts the user for a word and reads it,

● converts all characters of the input word to upper case and displays every character whose index is a multiple of 3 starting from the 1st one,

● next converts all characters of the input word to lower case and displays the word with the characters in reverse order,

● finally displays the original word as it was entered by the user.

Based on the previous specifications your program should behave and look exactly as shown in the cases below. Your program should work for any word entered by the user, not just the ones

in the samples. Below illustrates how your program should behave and appear.

Example output: Enter◦a◦word:◦elephant↵

EPN↵

tnahpele↵

elephant

Enter◦a◦word:◦Nancy12A↵

NC2↵

a21ycnan↵

Nancy12A

Homework Answers

Answer #1

The program is given below. The comments are provided for the better understanding of the logic.

import java.util.Scanner; 

public class StringOperations{  
        public static void main(String[] args) {
                //Initialse the scanner object.
                Scanner sc = new Scanner(System.in); 
                
                //Display a message on the screen to enter a word.
                System.out.print("Enter a word: ");
                
                //Get the word from the command line.
                String word = sc.nextLine();

                //Convert the word to upper case and store it in a variable wordUpper
                String wordUpper = word.toUpperCase();
                
                //Loop from 0 to number of characters in the word-1.
        for (int i = 0; i < wordUpper.length(); i++) {
                        //Check of the index is divisible by 3.
                        //i % 3 will return 0 if a number is divisible  by 3.
                        if(i % 3 == 0) {
                                //Extract that character and print it, if the number is divisible by 3.
                                char c = wordUpper.charAt(i);
                                System.out.print(c);
                        }
        }       

                //Print a blank line.
                System.out.println("");
                
                String wordReversed = "";
                //Loop in the reverse order from number of characters in the word-1 to 0
        for (int i = word.length() - 1; i > -1; i--) {
                        //Extract the character and append it to the variable wordReversed
                        char c = word.charAt(i);
                        wordReversed = wordReversed + c;
        }
                //At the end of the above loop the variable wordReversed will have the reversed word.
                //Also convert the reversed word to lowercase.
                wordReversed = wordReversed.toLowerCase();
                System.out.println(wordReversed);               
                
                //Print the original word.
                System.out.println(word);
        }
}  

The screenshots of the code and output are provided below.

Know the answer?
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
Similar Questions
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a Java program and Flowchart that prompts the user to enter two characters and displays...
Write a Java program and Flowchart that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following characters are used to denote the majors: I: Information Management C: Computer Science A: Accounting
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
COBOL Translate English to Pig Latin This interactive program translates any word the user enters into...
COBOL Translate English to Pig Latin This interactive program translates any word the user enters into Pig Latin. The interactive session --------------------------------------------- Enter any word to see what it looks like in Pig Latin. To quit, type Uitqay. --------------------------------------------- string The Pig Latin equivalent is: Ingstray --------------------------------------------- unstring The Pig Latin equivalent is: Unstringlay --------------------------------------------- Uitqay Oodbyegay! Specifications • If the word starts with a consonant, move the consonants before the first vowel to the end of the word and...
use repl.it intro to C-programing no advance code also put good comment that i can know...
use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you program for a game of hangman. Store the word to be guessed as individual characters of an array called word. The player must guess the letters belonging to word. If the user enters a valid letter,...
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT