Java Program to Count Number of Words and Spaces in a String

This is the java program to count number of words and spaces in a string. I have taken initial value of word as 1 because if there is only one word in the string then there will be no space. The total spaces will be one less than the total number of words. It is a simple program and self explanatory, still if you are facing any problem to understand this then comment below. 


Output

Java Program to Count Number of Words and Spaces in a String

3 thoughts on “Java Program to Count Number of Words and Spaces in a String

  1. sudhir

    your countword program wrong because when we pass
    String str=” count number of words and sapces “;
    space in a starting then it gives wrong value

    Reply
  2. Muralidhar

    space at beginning or at end or at any place in the string gives wrong output..
    check it once….

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *