Thursday 6 September 2012

java program to find string matching in another string.


import java.io.*;

class paturn_match
{
       public static void main(String args[]) throws IOException
       {
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             char test[] = new char[100];
             char given_str[] = new char[15];
             String temp;
             int i,j,lentext,lenp,flag=0,count=0;
         
             System.out.print("Enter Text  =  ");
             temp = br.readLine();
             test = temp.toCharArray();
             lentext = temp.length();
         
             System.out.print("Enter Paturn  =  ");
             temp = br.readLine();
             given_str = temp.toCharArray();
             lenp = temp.length();

             for(i=0;i!=lentext-1;i++)
             {
                   j=i;
                   int k=0;
                   flag=0;
                         while((test[i] == given_str[k]) && (flag !=1))
                         {
                               j=j+1;
                               k=k+1;
                                    if(k>=(lenp-1))
                                    {
                                          flag=1;
                                          break;
                                    }
                         } //WHILE
                     
                         if((k>0) && (flag==1))
                         {
                               System.out.println(" Paturn found at  " + i);
                               count++;
                         }
            }  // FOR LOOP
        
            System.out.println(" count = " + count);
    }
} //paturn_match.java

output:


Enter Text  =  hello hello
Enter Paturn  =  he
 Paturn found at  0
 Paturn found at  6
 count = 2

Tuesday 4 September 2012

java program to use awt check box in applet.


// Demonstrate check boxes.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo" width=250 height=200>
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg = "";
Checkbox obj1, obj2, obj3, obj4;
public void init()
{
obj1 = new Checkbox("A",  false);
obj2 = new Checkbox("B",  false);
obj3 = new Checkbox("C",  true);
obj4 = new Checkbox("D",  false);
add(obj1);
add(obj2);
add(obj3);
add(obj4);
obj1.addItemListener(this);
obj2.addItemListener(this);
obj3.addItemListener(this);
obj4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
//repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g)
{
msg = "Current state: ";
g.drawString(msg, 6, 80);
msg = " A: " + obj1.getState();
g.drawString(msg, 6, 100);
msg = " B: " + obj2.getState();
g.drawString(msg, 6, 120);
msg = " C: " + obj3.getState();
g.drawString(msg, 6, 140);
msg = " D: " + obj4.getState();
g.drawString(msg, 6, 160);
}
}

Output:

java program to use awt check box group in applet.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CBGroup" width=250 height=200>
</applet>
*/
public class CBGroup extends Applet implements ItemListener
{
String msg = "";
Checkbox obj1, obj2, obj3, obj4;
CheckboxGroup cbg;

public void init()
{
cbg = new CheckboxGroup();
obj1 = new Checkbox("A", cbg, false);
obj2 = new Checkbox("B", cbg, false);
obj3 = new Checkbox("C", cbg, true);
obj4 = new Checkbox("D", cbg, false);
add(obj1);
add(obj2);
add(obj3);
add(obj4);
obj1.addItemListener(this);
obj2.addItemListener(this);
obj3.addItemListener(this);
obj4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g)
{
msg = "Current selection: ";
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
}
}
output: