Java: Count all words in a string
Count Words in String
Write a Java method to count all the words in a string.
Test Data:
Input the string: The quick brown fox jumps over the lazy
dog.
Pictorial Presentation:

Sample Solution:
Java Code:
Sample Output:
Input the string: The quick brown fox jumps over the lazy dog Number of words in the string: 9
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to count the number of words in a string without using the split() method.
- Write a Java program to count words in a string by treating punctuation marks as additional delimiters.
- Write a Java program to trim extra spaces from a string and then count the number of words.
- Write a Java program to count words in a string and then output the longest word found.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java method to count all vowels in a string.
Next: Write a Java method to compute the sum of the digits in an integer.
What is the difficulty level of this exercise?
Based on 204 votes, average difficulty level of this exercise is Easy
.
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics