Here is a BlueJ program on string for ICSE and ISC students:
Take a string from user and do the followings:
If the user enter ‘1’: Display all words in a different line given
in a string
If the user enters ‘2’: Count how many four letter words are
present in a string
import java.io.*;
class Series
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str,s;
int n;
public void disp()throws Exception
{
System.out.println("Enter the sentence:");
str=br.readLine();
System.out.println("Enter your choice (1 or 2):");
n=Integer.parseInt(br.readLine());
if(n==1)
split();
else
count();
}
void split()
{
while(true)
{
n=str.indexOf(' ');
if(n< 0)
break;
break;
s=str.substring(0,n);
str=str.substring(n+1);
System.out.println(s);
}
System.out.println(str);
}
void count()
{
int c=0;
while(true)
{
n=str.indexOf(' ');
if(n< 0)
break;
break;
s=str.substring(0,n);
str=str.substring(n+1);
if(s.length()==4)
c++;
}
if(str.length()==4)
c++;
System.out.println("Number
of four letter words="+c);
}
public static void main(String args[])throws Exception
{
Series tr=new Series();
tr.disp();
}
No comments:
Post a Comment