Answer to Question #299915 in Java | JSP | JSF for Nenter

Question #299915

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


1
Expert's answer
2022-02-19T16:56:32-0500


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();
	}
}

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