Friday, March 13, 2020

Sentence Word Count and Arranging Words In Alphabetic Order


For Details of The Program: CLICK HERE



import java.io.*;
import java.util.*;
class Matrixmult
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
String words[];
StringTokenizer stk;
int i,j,c,x;
public void take()throws Exception
{
int flag;
while(true)
{
flag=0;
System.out.println("Enter the sentence:");
str=br.readLine();
for(i=0;i < str.length();i++)
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
}
stk=new StringTokenizer(str," .,?!");
c=stk.countTokens();
x=0;
words=new String[c];
while(stk.hasMoreElements())
{
words[x++]=stk.nextToken();
}
System.out.println("Length="+c);
for(i=0;i < x-1;i++)
{
for(j=i+1;j < x;j++)
{
if(words[i].compareTo(words[j]) >0)
{
str=words[i];
words[i]=words[j];
words[j]=str;
}
}
}

System.out.println("\nRearranged Sentence:\n"); for(i=0;i < c;i++)
System.out.print(words[i]+" ");
}

public static void main(String args[]) throws Exception
 {
Matrixmult ob=new Matrixmult();
ob.take();
}
}







Same program using alternate technique.

import java.io.*;
import java.util.*;
class Matrixmult
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
String words[]=new String[20];
StringTokenizer stk;
int i,j,x;
public void take()throws Exception
{
int flag;
while(true)
{
flag=0;
System.out.println("\nEnter the sentence:");
str=br.readLine();
for(i=0;i < str.length();i++)
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
}
str=str.replace('.',' ');
str=str.replace(',',' ');
str=str.replace('?',' ');
str=str.replace('!',' ');
x=0; 
while(true)
{
i=str.indexOf(' ');
if(i < 0)
break;
words[x++]=str.substring(0,i);
str=str.substring(i+1);
}
words[x++]=str;

System.out.println("Length="+(x-1));
for(i=0;i < x-1;i++)
{
for(j=i+1;j < x;j++)
{
if(words[i].compareTo(words[j]) >0)
{
str=words[i];
words[i]=words[j];
words[j]=str;
}
}
}
System.out.println("\nRearranged Sentence:\n");
for(i=0;i < x;i++)
{
if(i==x-1)
System.out.print(words[i]);
else
System.out.print(words[i]+" ");
}
}

public static void main(String args[]) throws Exception {
Matrixmult ob=new Matrixmult();
ob.take();
}
}

2012 Computer Practical Paper: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner