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

Question #302333

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(),charAt(), indexOf(), substring() if required.

Array & split & ternary(:?) are not to be used


1
Expert's answer
2022-02-24T13:19:21-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.print(word + " " + word.length() + " ");
            if (word.length() > 1) {
                System.out.println(word.substring(word.length() - 1) + word.substring(1, word.length() - 1)
                        + word.substring(0, 1));
            } else {
                System.out.println(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