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

Question #299917

Write a program to take a string as input then display the words along with position of each character its below but don't take a white blank space as a separate character.

Example:

Enter a String

Hello World RUN

12345 67890 123


1
Expert's answer
2022-02-19T16:56:29-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