Skip to main content

count no. of words in java

// count no. of words in java

import java.io.*;
public class a
{
public static void main(String args[])throws Exception
{
String s ;
int i,count=0;

DataInputStream dd=new DataInputStream(System.in);
System.out.println("Enter a string");
s=dd.readLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
if(s.charAt(i)== ' ')
count+=1;
System.out.println("the no.of words are: "+count);

}

}

Comments

Popular posts from this blog

unix commands