Tuesday, February 12, 2019

String frequency ISC PRACTICAL

In this question it was required to find the frequency of each word reappearing words should not be shown again.
Eg-hydra killed the soldier killed?
Input -frequency count-
   hydra-1
   killed-2
   the-1
   soldier-1
  This program is quite tough.
Solutions
I have taken two strings st-it reads string from user.st3-it store the words that are not repeating themselves.at the end of while loop the value of st3 collected is again stored in st .St  gets small every time.





The program-
import java.util.*;
class string
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("give the string");
int N,len;
String st;
String dupst,st1,st2,st3;
N=in.nextInt();
if((N<1)||(N>5))
System.out.println("INvVALID");
st=in.nextLine().trim();
dupst=st;
st1=st2=st3="";
len=st.length();
int lst1,lst2,len2;
int j,freq=0;
j=lst1=lst2=0;
for(int i=0;i<len;i++)
{  
if((st.charAt(i)==' ')||(st.charAt(i)=='?')||(st.charAt(i)=='!'))
{  
st1=st.substring(lst1,i);//extract words
lst1=i+1;
lst2=0;
j=0;
st2="";
len2=dupst.length();
while(j<len2)

if((dupst.charAt(j)==' ')||(dupst.charAt(j)=='?')||(dupst.charAt(j)=='!'))
{   
     st2=dupst.substring(lst2,j);
lst2=j+1;
if(st1.compareTo(st2)==0)
{
freq++;


}
else

st3=st3+" "+st2;
}
}

j++;
}

if(freq>0)
{
System.out.println("the frequency of "+st1+" is  "+freq);
freq=0;
}
dupst=st3.trim()+" ";
st3="";
len2=dupst.length();
}
}
}
}

No comments:

Post a Comment