Sunday, February 23, 2014

Word Guessing Game using Java

import java.io.*;
import java.util.*;
class file4{
static int kc = 0;
public static void main(String []arg) throws IOException{
FileReader fr = new FileReader("D:\\java\\f1.txt"); 

    //the file should contain words separated by spaces !
BufferedReader br2 = new BufferedReader(fr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random r = new Random();

String st = br2.readLine();    

//Works for single line.Use while loop till null if file contains multiple lines.
//System.out.println(st);


String t[] = st.split(" ");
String t1= t[r.nextInt(t.length)];
char ch[] = new char[t1.length()];
char ch3[] = new char[t1.length()];
for(int j=0;j<t1.length();j++){
ch3[j]=t1.charAt(j);
}
String t2;

int i,k;

for(i=0;i<t1.length();i++){
ch[i]='-';
}

// System.out.println("\nenter ur guess");
char ch2;
String x2;
printchar(ch,t1.length());
while(true){
int j;
System.out.println("\nenter ur guess\n");
x2=br.readLine();
System.out.println();

if(scheck(x2,t1)==1)
for(j=0;j<x2.length();j++){

ch2=x2.charAt(j);
//ch2=(char)br.read();
for(k=0;k<t1.length();k++){

if(ch3[k]==ch2){
ch[k]=ch2;
}


}

}
printchar(ch,t1.length());
check2(ch,t1.length());


}
}


static void check2(char c[],int k){

int i,k1=0;
for(i=0;i<k;i++){
if(c[i]=='-'){
k1++;
}
}
kc++;
if(k1==0){
System.out.println(" is correct \nCongrats ! Guess has been completed in attempt no "+kc+"!");
System.exit(0);
}
else System.out.println("\tattempts so far :: "+kc);
//return;
//System.exit(0);
}
static void printchar(char c[],int k){
for(int i=0;i<k;i++)
System.out.print(c[i]);

}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}





Output




Saturday, February 8, 2014

PROGRAM TO BUILD EXPRESSION TREE USING STACK


 /*
Algorithm
  1 Take any postfix epression 
  2 Run the loop If it is an operand then simply push it to stack 
  3 Else if it is the operator then pop two operand  from the stack 
  4 Make the first and  second poped operand as a left child  and right child of current operand respectively then push it to stack 
  5 Continue the steps and run the loop till the length of postfix expression Anonymous Anonymous
*/

  • #include<stdio.h>
  • #include<stdlib.h>

  • #include<string.h>

  • #define SIZE 50

  • struct express_tree

  • {

  • char ch;

  • struct express_tree *left,*right;

  • };

  • struct stack

  • {

  • struct express_tree **array;

  • int top;

  • };

  • /*...................................................................*/

  • struct stack* create_stack()

  • {

  • struct stack *st=(struct stack *)malloc(sizeof(struct stack));

  • st->array=malloc(sizeof(struct express_tree)*SIZE);

  • return st;

  • }

  • /*...................................................................*/

  • int is_empty_stack(struct stack *s)

  • {

  • return(s->top==-1);

  • }

  • /*...................................................................*/

  • int is_full_stack(struct stack *s)

  • {

  • return(s->top==SIZE-1);

  • }

  • /*...................................................................*/

  • void push(struct stack *s,struct express_tree *new)

  • {

  • if(is_full_stack(s))

  • return;

  • s->array[++s->top]=new;

  • }

  • /*...................................................................*/

  • struct express_tree* pop(struct stack *s)

  • {

  • if(is_empty_stack(s))

  • return NULL;

  • return s->array[s->top--];

  • }

  • /*......................................................................*/

  • void delete_stack(struct stack *s)

  • {

  • if(s)

  • {

  • if(s->array)

  • free(s->array);

  • free(s);

  • }

  • }

  • /*......................................................................*/

  • void delete_express_tree(struct express_tree *expression)

  • {

  • if(expression)

  • {

  • delete_express_tree(expression->left);

  • delete_express_tree(expression->right);

  • free(expression);

  • }

  • }

  • /*......................................................................*/

  • void display_tree(struct express_tree *root)

  • {

  • if(root)

  • {

  • display_tree(root->left);

  • display_tree(root->right);

  • printf("%c\n",root->ch);

  • }

  • }

  • /*......................................................................*/

  • struct stack* tree_making(char str[])

  • {

  • struct stack *s=create_stack();

  • struct express_tree *new_node,*t2,*t1,*new;

  • int i;

  • for(i=0;i<strlen(str);i++)

  • {

  • if(isalpha(str[i]))

  • {

  • new_node=malloc(sizeof(struct express_tree));

  • new_node->ch=str[i];

  • new_node->left=new_node->right=NULL;

  • push(s,new_node);

  • }

  • else

  • {

  • t2=pop(s);

  • t1=pop(s);

  • new=malloc(sizeof(struct express_tree));

  • new->ch=str[i];

  • new->left=t1;

  • new->right=t2;

  • push(s,new);

  • }

  • }

  • return s;

  • }

  • /*......................................................................*/

  • int main()

  • {

  • struct express_tree *temp;

  • struct stack *s;

  • char str[SIZE];

  • printf("\nenter any postfix expression ");

  • fgets(str,SIZE,stdin);

  • s=tree_making(str);

  • temp=pop(s);

  • printf("expression tree\n");

  • display_tree(temp);

  • delete_express_tree(temp);

  • delete_stack(s);

  • return 0;

  • }

Wednesday, February 5, 2014

Command line inputs in Java : Program to count numbers

class q3{
public static void main(String []s){
int i=0,c1=0,c2=0,c3=0;
int k[] = new int[s.length];
for(i=0;i<s.length;i++)
 k[i] =Integer.parseInt(s[i]);
 
i=0; 
while(i!=k.length){
if(k[i]%2==0){
c1++;
}
if(k[i]%2!=0){
c2++;
}
if(k[i]%2==0){
c3+=k[i];
}
i++;
}
System.out.println("even nos = " + c1);
System.out.println("odd nos = " + c2);
System.out.println("sum of evennos = " + c3);
}
}



/*
suppose the file name is q3.java
javac q3.java
java q3 3 4 2 1 6            //inputs
OUTPUT
even nos = 3
odd nos = 3
sum of evennos = 12
*/


Conversion of positive integer to Binary number in Java using arrays


class q1{ 
 public static void main(String []s){ 
           q2 q = new q2();
           q.convert(9);      //any input
           q.display(); 
          } 

class q2{ 
        int bin[] = new int[8]; //output has been considered as 8 bit
        int i=7; 
        void convert(int k){
             int t=k;
              while(t>0){
                  int r=t%2;
                  t/=2;
                  bin[i--]=r;
              }
         } 
       void display(){
              int j=0;
              while(j!=8){
                System.out.print(bin[j++]); 
               }
              System.out.println();
         }
 }
/*
OUTPUT 
00001001
*/