Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.util.Scanner;

public class WordCount {
	 public static void main(String args[])
	    {
	        String text;
	        int countWords=0;
	         
	        Scanner SC=new Scanner(System.in);
	         
	        System.out.print("Enter string: ");
	        text=SC.nextLine();
	         
	        //word count
	        for(int i=0; i<text.length()-1; i++)
	        {
	            if(text.charAt(i)==' ' && text.charAt(i+1)!=' ')
	                countWords++;
	        }
	         
	        System.out.println("Total number of words in string are: "+ (countWords+1));
	        //since last word does not contain and character after that
	                     
	    }
}


What I have tried:

I have got the above code to count the number of words in a string.

2 cases I have tried:

case 1 :
Enter string: hello java
Total number of words in string are: 2


case 2 :
Enter string:    hello   java
Total number of words in string are: 3


Case 1 is giving expected answer. But for case 2 I have inserted spaces at the beginning and in between the words. I got result as 3, but expected is 2 .

According to the login the count should be incremented if the current character is space and next character is not space. Can anyone explain this?
Posted
Updated 21-Sep-19 22:54pm

1 solution

You are counting each space as indicating that the next character begins a new word. So if a space follows a space you count that as a word. You need to rework your algorithm to ignore repeated spaces after the first, and only start a new count when a non-space is encountered.
 
Share this answer
 
Comments
radhikay 22-Sep-19 3:52am    
I got a solution to remove the spaces using trim() method. - which gives me correct answer for case 2

But as per your solution "only start a new count when a non-space is encountered" I have already used that logic as below
if(text.charAt(i)==' ' && text.charAt(i+1)!=' ')
the count gets incremented only when the current index in space and next index is non-space.
Richard MacCutchan 22-Sep-19 4:35am    
You are only dealing with the case when there are two spaces. What about the case when there are three spaces, or three hundred?
radhikay 22-Sep-19 4:38am    
I am having problem with spaces at the beginning of sentence. so trim() method will help me.

Please tell me the solution, I am not getting idea
Richard MacCutchan 22-Sep-19 5:17am    
You need to use a flag so you know whether you are getting repeated spaces or non-spaces. If the last character was a space and the current character is a non-space, then add 1 to the word count and flip the flag. If the current character is space and the last was a non-space then flip the flag, but do not add to the word count. You can do this with two simple loops.
radhikay 22-Sep-19 5:19am    
I will try this . thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900