CALCULATE Number of WORDS in given String.
JAVA Code :)
import java.util.Scanner;
public class Word
{
public static void main(String[] args)
{
String str;
int c=1;
Scanner s=new Scanner(System.in);
System.out.print("Enter a String:");
str=s.nextLine();
s.close();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==32 || str.charAt(i)=='\n' || str.charAt(i)=='\t')
{
c+=1;
}
}
System.out.print("Number of words in String are = "+c);
}
}
0 Comments