October 12, 2018

Sreekanth B

Write a Java program to count the number of words present in a string

Write a Java program to count the number of words present in a string?

Program:

  public class Test 
{
    public static void main (String args[])
    {
        String s = "Sharma is a good player and he is so punctual";
        String words[] = s.split(" ");
        System.out.println("The Number of words present in the string are : "+words.length);
    }
}

Output

The Number of words present in the string are : 10




Subscribe to get more Posts :