import java.io.*;
import java.awt.*;
import java.awt.ScrollPane;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.lang.String;
import java.util.Arrays;

public class Sad extends Applet implements ActionListener {
CB cb;
TextArea textInput;     // For the user's input text.
public void init()
{
			 cb = new CB("OUTPUT", 800,800);
             setBackground(Color.darkGray);
			 textInput = new TextArea();
	         textInput.setBackground(Color.white);
			 Panel south = new Panel();
		     south.setLayout( new GridLayout(4,1,2,2) );
			 

			Label l = new Label("Enter the strings seperated by $ sign and press process the text");
			 l.setBackground(Color.lightGray);
			 
			 south.add(l);

			 Button countButton = new Button("Process the Text");
			 countButton.setBackground(Color.lightGray);
			 countButton.addActionListener(this);
			 south.add(countButton);


             setLayout( new BorderLayout(2,2) );
			 add(textInput, BorderLayout.CENTER);
			 add(south, BorderLayout.SOUTH);
} // end init();
public Insets getInsets()
{
	   return new Insets(2,2,2,2);
}
public void actionPerformed(ActionEvent evt) 
{
	 String text; 
	 int charCt, wordCt, lineCt=0;  
	 text = textInput.getText();
	 charCt = text.length();
	 for (int i = 0; i < charCt; i++)
     {
	  if (text.charAt(i) == '$')
	  {
	   lineCt++;
	  }
     }
	String strs[] = new String[lineCt+1];
	int j=0;
	StringTokenizer st = new StringTokenizer(text,"$");
	while (st.hasMoreTokens()) 
	{ 
		String temp=st.nextToken();
		strs[j]=temp;
		j++;
	} 
	cb.v("\n");
	cb.v("Input************");
	cb.v("\n");
	for (int i = 0; i < j; i++)
   {
		cb.v("\n");
		cb.v(strs[i]);
		cb.v("\n");
   }

// code to shift
// finding wct
int n=0;
int words[] = new int[lineCt+1];
for(int k=0;k<lineCt+1;k++)
{
	for(int p=0;p<strs[k].length();p++)
	{
			if(strs[k].charAt(p)==' ')
				
		{
			words[k]++;
			n--;
		}n++;
	}

}

String strs2[] = new String[n];
String msg,msg1,req;
int x=0,z=0;

for(int strno=0;strno<lineCt+1;strno++)
{
for(int k=0;k<words[strno]+1;k++)
{
StringTokenizer st1 = new StringTokenizer(strs[strno]);
{ 
strs2[x]=strs[strno];
String temp1=st1.nextToken();
x++;
strs2[x]=temp1;
int len1 = temp1.length();
int len2 = strs[strno].length();
msg1 = strs[strno].substring(len1+1,len2);
req = msg1 + " " + temp1;
strs2[z]=strs[strno];
z++;
strs2[z]=req;
strs[strno]=req;
}
}
}
	cb.v("\n");
cb.v("*****After circular shift***** " );
	cb.v("\n");
for(int y1=0;y1<z;y1++)
{
		cb.v("\n");
		cb.v(strs2[y1]);
		cb.v("\n");
}
String[] pavs=new String[z];
for(int p=0;p<z;p++)
	pavs[p]=strs2[p];
	cb.v("\n");
cb.v("*****After sorting*****");
	cb.v("\n");
Arrays.sort(pavs);
	for (int i = 0; i < z; i++)
		{
			cb.v("\n");
			cb.v(pavs[i]);
			cb.v("\n");
		}
	}          
} // end class TextCounterApplet

class CB extends Frame 	
{
	Color background = Color.red;	
	Color text = Color.white;	
	int no_of_strings = 1200;		
	int line_space = 10;		

	Panel fp;
	Image img, oldimg;
	String str[] = new String[no_of_strings];
	int wide, high, laststring = -1, oldlaststring;
  ScrollPane scroller;

	CB(String title, int w, int h)
	{
		wide = w; high = h;
		show();
		hide();
		setTitle(title);
		
		scroller = new ScrollPane( ScrollPane.SCROLLBARS_ALWAYS );
		
		fp = new Panel();
		fp.setForeground(text);
		fp.setBackground(background);
		Insets ins;
		ins = new Insets(insets().top, insets().left, insets().bottom, insets().right);
		resize(wide + ins.left + ins.right, high + ins.top + ins.bottom);
		
fp.resize(wide, high);
	fp.setLayout( new GridLayout(1000,1000) );
add(scroller);
	fp.add( new Label( "*****Input*****" ) ); 
scroller.add ( fp );
scroller.doLayout(); 
}



	public void v(String s)				
	{
		if (laststring + 1 == no_of_strings)	
		{
			for (int i = 0; i < laststring; i++)
			{
				str[i] = str[i+1];
			}
			wipe();
		}
		else
			++laststring;
			
		str[laststring] = s;
		show();
				
	}

	

	public void clear()				// example usage: cb.clear();
	{
		laststring = -1;			// output: <an empty comment box>
		img = null;
		wipe();
	}

	// private display methods...
	
	private void wipe()
	{
		Graphics fg = fp.getGraphics();
		fg.setColor(background);
		fg.fillRect(0, 0, wide, high);
	}

	

	private void displayText()// text is written over the image (if any)
	{
		Graphics fg = fp.getGraphics();
		int i;
		for (i = 0; i <= laststring; i++)
			fg.drawString(str[i], 5, 3+line_space+i*line_space);
		repaint();
	}

	public void paint(Graphics g)
	{
		
		if (laststring != -1) displayText();
	}

	// event handling methods...
	public boolean handleEvent(Event evt)
	{
		switch (evt.id)
		{
			case Event.WINDOW_DESTROY:
				hide();

				return true;
			default:
				return super.handleEvent(evt);
		}			 
	}

	public boolean mouseDown(Event evt, int x, int y) 
	{
		repaint();				// mouseclick redraws
		return true;				// the comment box
	}

	public boolean keyDown(Event evt, int nKey)
	{
		if ((nKey == 90) || 			// z, Z or CTRL+z key
			(nKey == 122) ||		// restores contents
			(nKey == 26))			// if pressed immediately
		{					// after they have been
			img = oldimg;			// erased (see below)
			laststring = oldlaststring;
			paint(getGraphics());
		}
		else					// any other key (after
		{					// a mouseclick) clears
			oldimg = img;			// the comment box and
			oldlaststring = laststring;	// erases its contents
			clear();
		}
		return true;
	}

}// 