Answer to Question #300953 in Java | JSP | JSF for Caser

Question #300953

Write a program to take a String as input then display the words in one column, its length in another column, replace the first with last character of each word in third column.

Enter a String

a is the first vowel

a 1 a

is 2 si

the 3 eht

first 5 tirsf

vowel 5 lowev

Hint: Use length(), indexOf(), substring() if required.

Array & split are not to be used


1
Expert's answer
2022-02-22T06:46:06-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a String");
        String line = in.nextLine();
        while (true) {
            int space = line.indexOf(' ');
            String word = line.substring(0, space == -1 ? line.length() : space);
            System.out.println(word + " " + word.length() + " " + (word.length() > 1 ? word.substring(word.length() - 1)
                    + word.substring(1, word.length() - 1) + word.substring(0, 1) : word));
            line = line.substring(space + 1);
            if (space == -1) {
                break;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS