Answer to Question #296299 in Java | JSP | JSF for Greta

Question #296299

Write a program to take a String as input then display the first and last letter of each word with a condition that if it is having one character then it will remain as it is using only indexOf() function.

For example:

Enter a String

ASia is A conTinEnT

Modified: Aa is A cT


1
Expert's answer
2022-02-10T14:00:56-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[] data = in.nextLine().split(" ");
        System.out.print("Modified: ");
        for (int i = 0; i < data.length; i++) {
            if (data[i].length() == 1) {
                System.out.print(data[i]);
            } else {
                System.out.print(data[i].charAt(0) + "" + data[i].charAt(data[i].length() - 1));
            }

            if (i < data.length - 1) {
                System.out.print(" ");
            } else {
                System.out.println();
            }
        }
    }
}

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