class seven_iteration_number
{
public int deasc(int n)
{
int t,a,i,no=0;
for(i=9;i>=0;i--)
{
t=n;
while(t>0)
{
a=t%10;
if(a == i)
no=(no*10)+a;
t/=10;
}
}
return(no);
}
public int asc(int n)
{
int t,a,i,no=0;
for(i=1;i<10;i++)
{
t=n;
while(t>0)
{
a=t%10;
if(a == i)
no=(no*10)+a;
t/=10;
}
}
return(no);
}
public int no_of_digit(int t)
{
int c=0;
while(t>0)
{
c++;
t/=10;
}
return(c);
}
public void chk_seven_iteration_number(int n)
{
int as,ds,dig,dig1,diff;
do
{
as=asc(n);
ds=deasc(n);
diff=ds-as;
n=diff;
if(diff == 6174)
{
System.out.println(ds + "\t - \t"+as+"\t = \t " +diff);
break;
}
else
System.out.println(ds + "\t - \t"+as+"\t = \t " +diff);
}while(true);
}
}
Wednesday, December 1, 2010
pRG FOR IMPLEMENT STACK WITH ARRAY USING JAVA
import java.io.*;
class Stack
{
int arr[]=new int[10];
int len=0;
Stack()
{
for(int i=0;i<10;i++)
arr[i]=0;
}
void push(int a)
{
if(len<10)
{
arr[len]=a;
len++;
}
else System.out.println("Cannot Input more than 10 Numbers");
}
int pop()
{
if(len>0)
return arr[len-1];
else return -1;
}
int popAndTop()
{
if(len>0)
return arr[len--];
else return -1;
}
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
Stack pile=new Stack();
do
{
System.out.println("\tList\n1.Push\n2.Pop\n3.Pop And Top\n4.Exit");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:System.out.println("Enter Element");
int n=Integer.parseInt(br.readLine());
pile.push(n);
break;
case 2:if(pile.pop()!=-1)
System.out.println("\t"+pile.pop());
else System.out.println("No Elements to Display");
break;
case 3:if(pile.popAndTop()!=-1)
System.out.println("\t"+pile.popAndTop());
else System.out.println("No Elements to Display");
break;
case 4:System.out.println("Goodbye");break;
default:System.out.println("Invalid choice");
}
}while(ch!=4);
}
}
class Stack
{
int arr[]=new int[10];
int len=0;
Stack()
{
for(int i=0;i<10;i++)
arr[i]=0;
}
void push(int a)
{
if(len<10)
{
arr[len]=a;
len++;
}
else System.out.println("Cannot Input more than 10 Numbers");
}
int pop()
{
if(len>0)
return arr[len-1];
else return -1;
}
int popAndTop()
{
if(len>0)
return arr[len--];
else return -1;
}
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
Stack pile=new Stack();
do
{
System.out.println("\tList\n1.Push\n2.Pop\n3.Pop And Top\n4.Exit");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:System.out.println("Enter Element");
int n=Integer.parseInt(br.readLine());
pile.push(n);
break;
case 2:if(pile.pop()!=-1)
System.out.println("\t"+pile.pop());
else System.out.println("No Elements to Display");
break;
case 3:if(pile.popAndTop()!=-1)
System.out.println("\t"+pile.popAndTop());
else System.out.println("No Elements to Display");
break;
case 4:System.out.println("Goodbye");break;
default:System.out.println("Invalid choice");
}
}while(ch!=4);
}
}
pRG FOR READ A STRING AND CONVERT ALL VOWEL TO ITS NEXT VOWEL AND ALL CONSONENT TO ITS NEXT CONSONENT
class strings
{
void chk(String st)
{
int i,j,k,m;
i=st.length();
char ch;
String wrd="";
Character arr[]={'a','e','i','o','u'};
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch=='z')
wrd+='b';
else if(!(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
{
k=(int)ch;
k++;
if(((char)k=='a'||(char)k=='e'||(char)k=='i'||(char)k=='o'||(char)k=='u'))
{
k=(int)(char)k;
k++;
wrd+=(char)(int)(char)k;
}
else
wrd+=(char)k;
}
else
{
for(m=0;m<5;m++)
{ if(ch==arr[m] && ch!=arr[0])
wrd+=arr[m-1];
}
if(ch==arr[0])
wrd+=arr[4];
}
}
System.out.println(wrd);
}
}
{
void chk(String st)
{
int i,j,k,m;
i=st.length();
char ch;
String wrd="";
Character arr[]={'a','e','i','o','u'};
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch=='z')
wrd+='b';
else if(!(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
{
k=(int)ch;
k++;
if(((char)k=='a'||(char)k=='e'||(char)k=='i'||(char)k=='o'||(char)k=='u'))
{
k=(int)(char)k;
k++;
wrd+=(char)(int)(char)k;
}
else
wrd+=(char)k;
}
else
{
for(m=0;m<5;m++)
{ if(ch==arr[m] && ch!=arr[0])
wrd+=arr[m-1];
}
if(ch==arr[0])
wrd+=arr[4];
}
}
System.out.println(wrd);
}
}
Prog for read a number and display all sets of sum of concetuive number
class sum_consucative
{
public void chk_sum_consucative(int n)
{
int i,j,t,f,chk=0,a;
for(i=1;i<n;i++)
{
t=0;
f=0;
for(j=i;j<=n;j++)
{
t+=j;
if( t == n)
{
chk=1;
f=1;
break;
}
else if(t>n)
break;
}
if( f == 1)
{
for(a=i;a<j;a++)
System.out.print(a+" + ");
System.out.print(a + " = " + n);
System.out.println();
}
}
if( chk == 0)
System.out.print("Sorry set not possiable................");
}
}
{
public void chk_sum_consucative(int n)
{
int i,j,t,f,chk=0,a;
for(i=1;i<n;i++)
{
t=0;
f=0;
for(j=i;j<=n;j++)
{
t+=j;
if( t == n)
{
chk=1;
f=1;
break;
}
else if(t>n)
break;
}
if( f == 1)
{
for(a=i;a<j;a++)
System.out.print(a+" + ");
System.out.print(a + " = " + n);
System.out.println();
}
}
if( chk == 0)
System.out.print("Sorry set not possiable................");
}
}
Prog for display tic tac toe using java
import java.io.*;
class tictactoe
{
char arr[][]={{'_','_','_'},{'_','_','_'},{'_','_','_'}};
public void display()
{
int r,c;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
System.out.print(arr[r][c]+"\t");
System.out.println();
}
}
public int iswin()
{
int k=0;
if((arr[0][1]=='o'&&arr[0][2]=='o')||(arr[1][0]=='o'&&arr[2][0]=='o'))
k=1;
if((arr[0][0]=='x' && arr[0][1]=='x' && arr[0][2]=='x')||(arr[0][0]&&arr[1][0]=='x' && arr[2][0]=='x'))
k=2;
if((arr[2][2]=='o'&&arr[0][2]=='o'&&arr[1][2]=='o')||(arr[0][0]&&arr[2][0]=='o'&&arr[2][1]=='o'))
k=1;
if((arr[2][2]=='x'&&arr[0][2]=='x'&&arr[1][2]=='x')||(arr[0][0]&&arr[2][0]=='x'&&arr[2][1]=='x'))
k=2;
if((arr[1][1]=='o'&&arr[0][0]=='o'&&arr[2][2]=='o')||(arr[1][1]=='o'&&arr[2][0]=='o'&&arr[0][2]=='o')||
(arr[1][1]=='o'&&arr[0][1]=='o'&&arr[2][1]=='o')||(arr[1][1]=='o'&&arr[1][0]=='o'&&arr[1][2]=='o'))
k=1;
if((arr[1][1]=='x'&&arr[0][0]=='x'&&arr[2][2]=='x')||(arr[1][1]=='x'&&arr[2][0]=='x'&&arr[0][2]=='x')||
(arr[1][1]=='x'&&arr[0][1]=='x'&&arr[2][1]=='x')||(arr[1][1]=='x'&&arr[1][0]=='x'&&arr[1][2]=='x'))
k=2;
return k;
}
public void main()throws IOException
{
int i,r,c;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter player 1 ");
String n1=br.readLine();
System.out.println("Enter player 2 ");
String n2=br.readLine();
System.out.println(n1+" gets o");
System.out.println(n2+" gets x");
for(i=1;i<=5;i++)
{
System.out.println(n1+"'s turn");
System.out.println("Enter row");
r=Integer.parseInt(br.readLine());
System.out.println("Enter column");
c=Integer.parseInt(br.readLine());
if(arr[r][c]!='_')
{
System.out.println("Position occupied");
break;
}
else
arr[r][c]='o';
display();
if(iswin()==1)
{
System.out.println(n1+" wins!");
break;
}
else if(iswin()==2)
{
System.out.println(n2+" wins!");
break;
}
if(i==5)
break;
System.out.println(n2+"'s turn");
System.out.println("Enter row");
r=Integer.parseInt(br.readLine());
System.out.println("Enter column");
c=Integer.parseInt(br.readLine());
if(arr[r][c]!='_')
{
System.out.println("Position occupied");
break;
}
else
arr[r][c]='x';
display();
}
if(iswin()==0)
System.out.println("Draw!!!");
}
}
class tictactoe
{
char arr[][]={{'_','_','_'},{'_','_','_'},{'_','_','_'}};
public void display()
{
int r,c;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
System.out.print(arr[r][c]+"\t");
System.out.println();
}
}
public int iswin()
{
int k=0;
if((arr[0][1]=='o'&&arr[0][2]=='o')||(arr[1][0]=='o'&&arr[2][0]=='o'))
k=1;
if((arr[0][0]=='x' && arr[0][1]=='x' && arr[0][2]=='x')||(arr[0][0]&&arr[1][0]=='x' && arr[2][0]=='x'))
k=2;
if((arr[2][2]=='o'&&arr[0][2]=='o'&&arr[1][2]=='o')||(arr[0][0]&&arr[2][0]=='o'&&arr[2][1]=='o'))
k=1;
if((arr[2][2]=='x'&&arr[0][2]=='x'&&arr[1][2]=='x')||(arr[0][0]&&arr[2][0]=='x'&&arr[2][1]=='x'))
k=2;
if((arr[1][1]=='o'&&arr[0][0]=='o'&&arr[2][2]=='o')||(arr[1][1]=='o'&&arr[2][0]=='o'&&arr[0][2]=='o')||
(arr[1][1]=='o'&&arr[0][1]=='o'&&arr[2][1]=='o')||(arr[1][1]=='o'&&arr[1][0]=='o'&&arr[1][2]=='o'))
k=1;
if((arr[1][1]=='x'&&arr[0][0]=='x'&&arr[2][2]=='x')||(arr[1][1]=='x'&&arr[2][0]=='x'&&arr[0][2]=='x')||
(arr[1][1]=='x'&&arr[0][1]=='x'&&arr[2][1]=='x')||(arr[1][1]=='x'&&arr[1][0]=='x'&&arr[1][2]=='x'))
k=2;
return k;
}
public void main()throws IOException
{
int i,r,c;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter player 1 ");
String n1=br.readLine();
System.out.println("Enter player 2 ");
String n2=br.readLine();
System.out.println(n1+" gets o");
System.out.println(n2+" gets x");
for(i=1;i<=5;i++)
{
System.out.println(n1+"'s turn");
System.out.println("Enter row");
r=Integer.parseInt(br.readLine());
System.out.println("Enter column");
c=Integer.parseInt(br.readLine());
if(arr[r][c]!='_')
{
System.out.println("Position occupied");
break;
}
else
arr[r][c]='o';
display();
if(iswin()==1)
{
System.out.println(n1+" wins!");
break;
}
else if(iswin()==2)
{
System.out.println(n2+" wins!");
break;
}
if(i==5)
break;
System.out.println(n2+"'s turn");
System.out.println("Enter row");
r=Integer.parseInt(br.readLine());
System.out.println("Enter column");
c=Integer.parseInt(br.readLine());
if(arr[r][c]!='_')
{
System.out.println("Position occupied");
break;
}
else
arr[r][c]='x';
display();
}
if(iswin()==0)
System.out.println("Draw!!!");
}
}
Prog for convert time in word format in java
import java.io.*;
class TimeInWords
{
public void display_time()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter hours.....(1-12)");
int hh=Integer.parseInt(br.readLine());
System.out.println("Enter minutes.....(0-59)");
int mm=Integer.parseInt(br.readLine());
String time[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine",
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen","Twenty","Twenty One","Twenty Two","Twenty Three",
"Twenty Four","Twenty Five","Twenty Six","Twenty Seven","Twenty Eight",
"Twenty Nine"};
if(hh>12 || mm>60)
System.out.println("The Time is invalid");
else if(mm==0)
System.out.println("The time is "+time[hh-1]+" o'Clock");
else if(mm<30 && mm!=15)
System.out.println("The time is "+time[mm-1]+" minute(s) past "+time[hh-1]);
else if(mm==15)
System.out.println("The time is quarter past "+time[hh-1]);
else if(mm==30)
System.out.println("The time is "+time[hh-1] + " Thirty");
else if(mm==45 && hh!=12)
System.out.println("The time is quarter to "+time[hh]);
else if(mm==45 && hh==12)
System.out.println("The time is quarter to "+time[0]);
else if(mm>30 && hh!=12)
System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[hh]);
else if(hh==12 && mm>30)
System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[0]);
}
}
class TimeInWords
{
public void display_time()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter hours.....(1-12)");
int hh=Integer.parseInt(br.readLine());
System.out.println("Enter minutes.....(0-59)");
int mm=Integer.parseInt(br.readLine());
String time[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine",
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen","Twenty","Twenty One","Twenty Two","Twenty Three",
"Twenty Four","Twenty Five","Twenty Six","Twenty Seven","Twenty Eight",
"Twenty Nine"};
if(hh>12 || mm>60)
System.out.println("The Time is invalid");
else if(mm==0)
System.out.println("The time is "+time[hh-1]+" o'Clock");
else if(mm<30 && mm!=15)
System.out.println("The time is "+time[mm-1]+" minute(s) past "+time[hh-1]);
else if(mm==15)
System.out.println("The time is quarter past "+time[hh-1]);
else if(mm==30)
System.out.println("The time is "+time[hh-1] + " Thirty");
else if(mm==45 && hh!=12)
System.out.println("The time is quarter to "+time[hh]);
else if(mm==45 && hh==12)
System.out.println("The time is quarter to "+time[0]);
else if(mm>30 && hh!=12)
System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[hh]);
else if(hh==12 && mm>30)
System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[0]);
}
}
Transpose array without any other array in java
import java.io.*;
class transpose_1array
{
public void transpose()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,t=0;
System.out.println("Enter the size of the array");
int n=Integer.parseInt(br.readLine());
int arr[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.println("Enter element at "+i+","+j);
arr[i][j]=Integer.parseInt(br.readLine());
}
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
t=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=t;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
class transpose_1array
{
public void transpose()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,t=0;
System.out.println("Enter the size of the array");
int n=Integer.parseInt(br.readLine());
int arr[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.println("Enter element at "+i+","+j);
arr[i][j]=Integer.parseInt(br.readLine());
}
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
t=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=t;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
To display all union ele from two different single dimension array in java
import java.io.*;
class union
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of array1 :- ");
int s1=Integer.parseInt(br.readLine());
System.out.print("Enter the size of array2 :- ");
int s2=Integer.parseInt(br.readLine());
int a[]=new int[s1];
int b[]=new int[s2];
int c[]=new int[s1+s2];
int i,j,pos1=0,pos2=0,pos3=0,f=0;
System.out.print("Enter elements of array1 ");
for(i=0;i<s1;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.print("Enter elements of array2 ");
for(i=0;i<s2;i++)
b[i]=Integer.parseInt(br.readLine());
for(i=0;i<s1;i++)
{f=0;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
f=1;
break;
}
}
if(f==0)
{
a[pos1++]=a[i];
c[pos3++]=a[i];
}
}
for(i=0;i<s2;i++)
{f=0;
for(j=0;j<i;j++)
{
if(b[i]==b[j])
{
f=1;
break;
}
}
if(f==0)
b[pos2++]=b[i];
}
for(i=0;i<pos2;i++)
{f=0;
for(j=0;j<pos1;j++)
{
if(b[i]==a[j])
{
f=1;
break;
}}
if(f==0)
c[pos3++]=b[i];
}
for(i=0;i<pos3;i++)
System.out.print(c[i]+" ");
}
}
class union
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of array1 :- ");
int s1=Integer.parseInt(br.readLine());
System.out.print("Enter the size of array2 :- ");
int s2=Integer.parseInt(br.readLine());
int a[]=new int[s1];
int b[]=new int[s2];
int c[]=new int[s1+s2];
int i,j,pos1=0,pos2=0,pos3=0,f=0;
System.out.print("Enter elements of array1 ");
for(i=0;i<s1;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.print("Enter elements of array2 ");
for(i=0;i<s2;i++)
b[i]=Integer.parseInt(br.readLine());
for(i=0;i<s1;i++)
{f=0;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
f=1;
break;
}
}
if(f==0)
{
a[pos1++]=a[i];
c[pos3++]=a[i];
}
}
for(i=0;i<s2;i++)
{f=0;
for(j=0;j<i;j++)
{
if(b[i]==b[j])
{
f=1;
break;
}
}
if(f==0)
b[pos2++]=b[i];
}
for(i=0;i<pos2;i++)
{f=0;
for(j=0;j<pos1;j++)
{
if(b[i]==a[j])
{
f=1;
break;
}}
if(f==0)
c[pos3++]=b[i];
}
for(i=0;i<pos3;i++)
System.out.print(c[i]+" ");
}
}
To find n digit unique number in java
import java.io.*;
class UniqueNo
{
long countDigits(long no)
{
long copy = no,count = 0;
while(copy!=0)
{
count++;
copy/=10;
}
return count;
}
boolean checkUnique(long n)
{
int nd = (int)countDigits(n);
long arr[] = new long[nd];
long copy = n;
int digit,term = 0;
while(copy!=0)
{
digit = (int)copy%10;
arr[term] = digit;
term++;
copy/=10;
}
for(int i = 0;i<nd-1;i++)
{
for(int j = i+1;j<nd;j++)
{
if(arr[i] == arr[j])
return false;
}
}
return true;
}
void main() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value of n to find all n digit unique nos - ");
long n = Long.parseLong(br.readLine());
long count = 0;
for(long i = (long)Math.pow(10,n-1);i<=(long)Math.pow(10,n)-1;i++)
{
if(checkUnique(i)==true)
{
System.out.println(i);
count++;
}
}
System.out.println(" "+count+" unique nos have been generated.");
}
}
class UniqueNo
{
long countDigits(long no)
{
long copy = no,count = 0;
while(copy!=0)
{
count++;
copy/=10;
}
return count;
}
boolean checkUnique(long n)
{
int nd = (int)countDigits(n);
long arr[] = new long[nd];
long copy = n;
int digit,term = 0;
while(copy!=0)
{
digit = (int)copy%10;
arr[term] = digit;
term++;
copy/=10;
}
for(int i = 0;i<nd-1;i++)
{
for(int j = i+1;j<nd;j++)
{
if(arr[i] == arr[j])
return false;
}
}
return true;
}
void main() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value of n to find all n digit unique nos - ");
long n = Long.parseLong(br.readLine());
long count = 0;
for(long i = (long)Math.pow(10,n-1);i<=(long)Math.pow(10,n)-1;i++)
{
if(checkUnique(i)==true)
{
System.out.println(i);
count++;
}
}
System.out.println(" "+count+" unique nos have been generated.");
}
}
Prg for omit common character from a string in java
class wihoutcommon
{
void find(String st)
{
int i,j,k,m, flag,f=0;
char ch,ch1,ch2;
String wrd="";
i=st.length();
for(j=0;j<i;j++)
{flag=0;
ch=st.charAt(j);
for(k=0;k<j;k++)
{
if(ch==st.charAt(k))
{
flag=1;
break;
}
}
if(flag==0)
{
for(m=j+1;m<i;m++)
{f=0;
if(ch==st.charAt(m))
{
f=1;
break;
}
}
if(f==0)
wrd+=ch;
}
}
System.out.println(wrd);
}
}
{
void find(String st)
{
int i,j,k,m, flag,f=0;
char ch,ch1,ch2;
String wrd="";
i=st.length();
for(j=0;j<i;j++)
{flag=0;
ch=st.charAt(j);
for(k=0;k<j;k++)
{
if(ch==st.charAt(k))
{
flag=1;
break;
}
}
if(flag==0)
{
for(m=j+1;m<i;m++)
{f=0;
if(ch==st.charAt(m))
{
f=1;
break;
}
}
if(f==0)
wrd+=ch;
}
}
System.out.println(wrd);
}
}
Prog for display frequency of each word fr a sentence in java
import java.io.*;
class word_freq
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence");
String st=br.readLine();
int i,j,k,count=0,c=0,m,n,flag=0;
char ch;
String wrd="";
st=st+" ";
i=st.length();
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch==' ')
count++;
}
String arr[]=new String[count];
int pos=0;
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch!=' ')
wrd+=ch;
else
{
arr[pos++]=wrd;
wrd="";
}
}
for(m=0;m<pos;m++)
{
flag=0;
for(n=0;n<m;n++)
{
if(arr[m].compareTo(arr[n]) == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
c=1;
for(k=m+1;k<pos;k++)
{
if(arr[m].compareTo(arr[k]) == 0)
c++;
}
System.out.println(arr[m]+" => "+c);
}
}
}
}
class word_freq
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence");
String st=br.readLine();
int i,j,k,count=0,c=0,m,n,flag=0;
char ch;
String wrd="";
st=st+" ";
i=st.length();
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch==' ')
count++;
}
String arr[]=new String[count];
int pos=0;
for(j=0;j<i;j++)
{
ch=st.charAt(j);
if(ch!=' ')
wrd+=ch;
else
{
arr[pos++]=wrd;
wrd="";
}
}
for(m=0;m<pos;m++)
{
flag=0;
for(n=0;n<m;n++)
{
if(arr[m].compareTo(arr[n]) == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
c=1;
for(k=m+1;k<pos;k++)
{
if(arr[m].compareTo(arr[k]) == 0)
c++;
}
System.out.println(arr[m]+" => "+c);
}
}
}
}
Subscribe to:
Posts (Atom)