Skip to content
October 7, 2013 / TDS Software

Java Q&A

Write a Java program to read a sentence which contains more than 10 words as a keyboard
input and to find the number of words in the sentence that starts with letter ‘i’. You must
use class StringTokenizer to break the sentence into words. Save your answer as
“Question12.java”

 

 

 

import java.io.*;
import java.util.StringTokenizer;
class Question12{
public static void main(String[] args) {
String inp =””;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter a Sentence : “);
inp = bf.readLine();

}catch (IOException e){
e.printStackTrace();
}

StringTokenizer st2 = new StringTokenizer(inp, ” “);
int count = 0;
while (st2.hasMoreElements()) {
String s = st2.nextElement().toString();
if (s.charAt(0)==’i’){
System.out.println(s);
count++;
}
}
System.out.println(“Number of words starting with ‘i’ : ” + count );
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: