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
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();
}
}
}
}
Comments
Leave a comment