144. JAVA Program to find number of words from the given text.
How does this program work?
- This program is used to find number of words from given text using java.
Here is the code
//To find number of words from given text
import java.util.Scanner;public class string
{
public static void main(String args[] )
{
int word=1;
String str;
System.out.println("Enter a string" );
Scanner obj =new Scanner(System.in );
str=obj.nextLine();
for(int i=0;i< str.length();++i)
{
if(str.charAt(i)==' ')
word++;
}
System.out.println("Number of words="+word);
}
}