Lab
13 Solutions, ESC101, 2004-2005 Semester-II
1.
import java.io.*;
public class RightRotate
{
char output[];
public char[] rotateRight(char arr[],int k,int
start,int end)
{
if( k>= (end - start +1) )
{
k = k %
(end-start+1);
}
if (k==0)
return arr;
if( k > arr.length/2)
{
output=rotateLeft(arr,end-start-k+1,start,end);
return output;
}
char temp;
for(int
cnt=end-start;cnt-k>=start;cnt--)
{
temp=arr[cnt];
arr[cnt]=arr[cnt-k];
arr[cnt-k]=temp;
}
int rem=(end-start+1) % k;
if(rem!=0)
output=rotateRight(arr,k-rem,start,start+k-1);
return arr;
}
public char[] rotateLeft(char arr[],int k,int
start,int end)
{
if( k>= (end - start +1) )
{
k = k %
(end-start+1);
}
if (k==0)
return arr;
if( k > arr.length/2)
{
output=rotateRight(arr,end-start-k+1,start,end);
return output;
}
char temp;
for(int
cnt=start;cnt+k<=end;cnt++)
{
temp=arr[cnt];
arr[cnt]=arr[cnt+k];
arr[cnt+k]=temp;
}
int rem=(end-start+1) % k;
if(rem!=0)
output=rotateLeft(arr,k-rem,end-k+1,end);
return arr;
}
public static void main(String args[]) throws
IOException
{
char temp[];
InputStreamReader in=new
InputStreamReader(System.in);
BufferedReader br=new
BufferedReader(in);
System.out.println("Enter the
String to be Rotated");
String s=br.readLine();
System.out.println("Enter by how
much to right rotate");
String s1=br.readLine();
int k=Integer.parseInt(s1);
RightRotate rr=new RightRotate();
if(k<0)
temp=rr.rotateLeft(s.toCharArray(),-k,0,s.length()-1);
else
temp=rr.rotateRight(s.toCharArray(),k,0,s.length()-1);
for(int i=0;i<temp.length;i++)
System.out.print(temp[i]);
System.out.println();
}
}
2.
import java.io.*;
import java.util.*;
public class WordFrequency
{
public static void wordFrequency(String InFile)
throws IOException
{
FileReader fin=new
FileReader(InFile);
BufferedReader br=new
BufferedReader(fin);
Hashtable words=new Hashtable();
for(;;)
{
String
s=br.readLine();
if(s==null)
break;
StringTokenizer st=new StringTokenizer(s,"
\t\n\r\f",true);
for(;st.hasMoreTokens();)
{
String temp=st.nextToken();
if(words.containsKey(temp))
words.put(temp, new Integer(
((Integer)words.get(temp)).intValue()+1 ));
else
words.put(temp,new
Integer(1));
}
}
String tempStr;
for (Enumeration e = words.keys()
; e.hasMoreElements() ;)
{
tempStr=e.nextElement().toString();
System.out.println("Frequency of Word " + tempStr + " " +
words.get(tempStr) );
}
}
public static void main(String args[]) throws
IOException
{
WordFrequency wf=new
WordFrequency();
wf.wordFrequency(args[0]);
}
}