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