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
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...
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...
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: ●...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
In Python write a program that prompts the user for six elements and their atomic numbers...
In Python write a program that prompts the user for six elements and their atomic numbers and stores them in a list. Each element and weight pair should also be a list. After the values have been entered, print the list as it entered. Then print the first two characters from the element names (the first character capitalized and the second in lower case) along with the atomic number, starting with the element with the lowest atomic number. You must...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT