Monday, August 1, 2011

BlueJ Program On Counting The Number Of Words From A Sentence


This is a program of string class. The entered sentence may contain extra blank spaces between the words and program has to count the number of words in the sentence.

How to proceed on this program



We will take the sentence from the user first. To remove leading and trailing blank spaces from a sentence, we can call ‘trim ()’ function. In this program we have to consider the extra spaces between the words also. Using a loop we have to eliminate unnecessary spaces from the sentence. The logic is very simple, no consecutive two spaces will be accepted. Spaces that appears after any alphabet or digit or punctuations, in a word spaces which appears after any non space characters are accepted and the entered sentence is modified and stored in another string object.

From the modified string object, the number of words are counted using an infinite loop. ‘indexOf ()’ function of string class is used to search space from the sentence. If space is found, the counter is incremented by one and the left word is removed from the sentence. We know that ‘indexOf ()’ function returns -1 if the argument string or character is not found in the invoking string. So when the function returns less than zero value, the loop is terminated.

Here is the codes of the program.

import java.io.*;
class String1
{
 String str,str1="";
 int i,len;
 boolean flag=false;
 char ch;
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
 public void takeString() throws Exception
 {
 int c=1;
  System.out.println("Enter the text:");
  str=br.readLine().trim();
 len=str.length();
 for(i=0;i< len;i++)
 {
 ch=str.charAt(i);
 if(ch==' ' && flag==true)
 {
  str1=str1+ch;
  flag=false;
 }
else  if(ch!=' ')
 {
  str1=str1+ch;
  flag=true;
 }
 }
  while(true)
  {
   i=str1.indexOf(' ');
   if(i<0)
   break;
   c++;
  str1=str1.substring(i+1);
  }
   System.out.println("\nNumber of words in the string="+c);
   }
   public static void main(String args[]) throws Exception
   {
    String1 ob=new String1();
    ob.takeString();
    }
   }


The same program can be done using StringTokenizer class of util package also and the program will be little bit simple than the above one.
  
import java.io.*;
import java.util.*;
class String1
{
 String str,str1="";
 int i,len;
 StringTokenizer stk;
 char ch;
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
 public void takeString() throws Exception
 {
 int c=1;
  System.out.println("Enter the text:");
  str=br.readLine().trim();
 len=str.length();
 for(i=0;i< len;i++)
 {
 ch=str.charAt(i);
 if(ch==' ' && flag==true)
 {
  str1=str1+ch;
  flag=false;
 }
else  if(ch!=' ')
 {
  str1=str1+ch;
  flag=true;
 }
 }
 stk=new StringTokenizer(str1);
 c=stk.countTokens();
   System.out.println("\nNumber of words in the string="+c);
   }
   public static void main(String args[]) throws Exception
   {
    String1 ob=new String1();
    ob.takeString();
    }
   }

 To know about StringTokenizer class, visit Few words on StringTokenizer class.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner