Friday, 20 April 2018



wap to count number of words in a sentences?
==================================================================

public class WordCount {

int  wordCount(String str)
{
int wc=0;

for(int i=0;i<str.length()-1;i++)

{
char ch=str.charAt(i);
if(ch==32)
{
wc++;
}
}

System.out.println("Total Word="+(wc+1));

return wc;

}

public static void main(String[] args) {
WordCount w=new WordCount();
w.wordCount("This is my india");
}}
=======================================================================

public class WordCount1 {
int  wordCount(String str)
{
int wc=0;
char[] c1=str.toCharArray();
for(int i=0;i<str.length()-1;i++)
{
if(c1[i]==32)
{
wc++;
}
}
System.out.println("Total Word="+(wc+1));
return wc;
}
public static void main(String[] args) {
WordCount1 w=new WordCount1();
w.wordCount("This is my india ");
}}

No comments:

Post a Comment