Sunday, January 26, 2014

Program On String Manipulation Without Using StringTokenizer Class


 

A sentence is terminated by either “ . ” , “ ! ” or “ ? ” followed by a space. Input a piece of text consisting of sentences. Assume that there will be a maximum of 10 sentences in a block.

 

Write a program to:

(i) Obtain the length of the sentence (measured in words) and

 the frequency of vowels in each sentence.

(ii) Generate the output as shown below using the given data

 

Sample data:

INPUT

HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.

OUTPUT

Sentence No. of Vowels No. of words

----------------------------------------------------------

1 2 1

2 5 3

3 8 4

4 3 3

 

 

Codes of the program

 

 

import java.io.*;

class Sent

{

 String str1,str2,str3[];

 int number,i;

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

 public void takeString()throws Exception

 {

  System.out.println("Enter the sentence along with punctuations:");

 str1=br.readLine();

  i=word(str1);

 str3=new String[i];

 i=0;

 

 while(true)

 {

 number=str1.indexOf(' ');

 if(number<0 o:p="">

 break;

 str3[i++]=str1.substring(0,number);

 str1=str1.substring(number+1);

 }

 str3[i++]=str1;

 number=i;

 System.out.println("\nSentence No   No. of vowels   No. of words\n");

 for(i=0;i

 {

  str1=str3[i];

  System.out.print((i+1)+"      ");

  System.out.print(vowel(str1)+ "      ");

  System.out.println(word(str1)+ "      ");

  }

  }

 

private int vowel(String s)

{

 int i,v=0,len;

 len=s.length();

 s=s.toUpperCase();

 for(i=0;i< len; i++)

 {

  if(s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')

  v++;

  }

  return v;

  }

 

private int word(String s)

{

 int i,v=1,len;

 len=s.length();

 for(i=0;i< len; i++)

 {

  if(s.charAt(i)==' ')

  v++;

  }

  return v;

  }

  public static void main(String args[])throws Exception

  {

   Sent object=new Sent ();

   object.takeString();

   }

   }

 

 

 


2 comments:

  1. Sir is string tokenizer in course. If not can we still use it in exams?

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner