Write a program to take a String as input then display the words and each character's position its below.
Example:
Enter a String
Elephant are large
12345678 91011 1213141516
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String inputString = in.nextLine();
int counter = 1;
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) != ' ') {
System.out.print(counter);
counter++;
}else {
System.out.print(" ");
}
}
in.close();
}
}
Comments
Leave a comment