214k views
4 votes
Write an application that counts the total number of spaces contained in a quote entered by the user.

1 Answer

2 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It asks the user for an input and saves it in a String variable. Then it loops through all the characters in the string and counts the spaces. Finally, it prints the total number of spaces in the String.

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter Line Now:");

String userInput = in.nextLine();

int spaceCount = 0;

for (int x = 0; x < userInput.length(); x++) {

if (userInput.charAt(x) == ' ') {

spaceCount++;

}

}

System.out.println(spaceCount);

}

User JTejedor
by
5.1k points