Thursday, 29 January 2015

Write a Program for Client and Server communication in java

server program: import java.net.*; import java.io.*; class Server { public static void main(String[] args) { try{ ServerSocket ss = new ServerSocket(5200); System.out.println("Server waiting....."); Socket s = ss.accept(); InputStreamReader is = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(is); String mc = br.readLine(); String key = "i am the passkey"; System.out.println(mc); StringBuilder sb = new StringBuilder(); for (int i=0;i<16;i++ ) { sb.append(mc.charAt(i)^key.charAt(i)); } System.out.println("The Message from client is"+sb.toString()); } catch(Exception e){System.out.println("Got an error "+e.getMessage());} } } client program import java.net.*; import java.io.*; class Client { public static void main(String[] args) { try{ Socket cs = new Socket("localhost",5200); OutputStreamWriter osw = new OutputStreamWriter(cs.getOutputStream()); BufferedWriter bw = new BufferedWriter(osw); String msg = "i am the message"; String key = "i am the passkey"; StringBuilder sb = new StringBuilder(); for (int i=0;i<msg.length() ;i++ ) { sb.append(msg.charAt(i)^key.charAt(i)); } bw.write(new String(sb)); bw.newLine(); bw.flush(); }catch(Exception e){ System.out.println("got an exception at client"+e.getMessage()); } } }

Wednesday, 7 January 2015

Write an algorithm to convert the given input string to the expected output string. The input string shall be the length of few million characters. So the output has to be updated in the same string...

Input String = "ABBCDEFGGGGGGGGHHXX
Expected output = "A1B2C1D1E1F1G8H2X2

#include <stdio.h> void main() { char *s="ABBCDEFGGGGGGGGHH"; char *ptr; char c; int count=1; ptr=s; c=*s; while(*ptr){ if(*ptr==c) { count++; ptr++; } else { printf("%c%d",c,count); count=0; c=*ptr; } } printf("%c%d",c,count); }

Given a large array of unsigned ints, quickly find two who's sum is 10

import java.util.*; public class AdditionDemo { public static void main(String[] args) { HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); int arr[]=new int[9]; for(int i=0;i<9;i++) arr[i]=i+1; for (int i:arr) { if(hm.get(i)==null) { if(hm.get(10-i)!=null) hm.put((10-i),i); else hm.put(i,0); } } System.out.println(hm); } }

Illustrate with an example the difference between deep and shallow copy in java?

import java.io.*; import java.util.*; public class Employee implements Cloneable,Serializable { int eno; String ename; Address addr; public Employee(int en,String name,Address a) { eno=en; ename=name; addr=a; } public Object clone() throws CloneNotSupportedException { Object o=null; try{ ByteArrayOutputStream baos=new ByteArrayOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois=new ObjectInputStream(bais); o=ois.readObject(); } catch(Exception e) { System.out.println(e.getMessage()); } return o; } } Address.java import java.io.*; import java.util.*; public class Address implements Cloneable,Serializable { String streetno; String city; public Address(String s,String c) { streetno=s; city=c; } } Test.java public class Test { public static void main(String[] args) { try { Address a1=new Address("123","Hyderabad"); Employee e1=new Employee(1,"Neil",a1); Employee e2=(Employee)e1.clone(); System.out.println(e2.addr.city); e1.addr.city="Mumbai"; e1.ename="shan"; System.out.println(e2.addr.city); System.out.println(e2.ename); } catch(Exception e) { System.out.println(e.getMessage()); } } }

change the value of the i without changing code of the main function, assign 20 to i ?

int fun() { //write a code here /* method 1 #define fun() i=20 */ //method 2 int j; int *ptr; ptr=&j; for (;*(ptr)!=10;ptr++); *ptr=20; } int main() { int i=10; fun(); //substitute i=20 printf("%d",i); }

Find the unique number that is present only once in array while all the others are present three times.

Example: 2,3,5,1,2,2,5,3,5,3
Answer : 1 as 2,3,5 are repeated three times Complexity should be better than O(nlogn)

#include <stdio.h> int dnr(int arr[],int n) { int i,j,count=0,result=0; int bit=1; for (i=0;i<32;i++) { count=0; for (j=0;j<n;j++ ) { if(arr[j] & bit) count++; } count%=3; if(count) result=result | bit; bit=bit<<1; } return result; } void main() { int a[]={17,14,11,14,17,21,17,21,21,14}; printf("%d\n",dnr(a,10)); }

Sunday, 7 December 2014

AVL Tree program in C

#include <stdio.h> #include<malloc.h> typedef enum{FALSE,TRUE}booleanx; struct anode *avlinsert(int,struct anode*,booleanx*); struct anode *avlsearch(struct anode*,int); void avldisplay(struct anode*,int); void avlinorder(struct anode*); struct anode { int info; //data int balance;//balance factor struct anode *lchild; struct anode *rchild; }; void main() { booleanx ht_inc; int info; int choice; system("cls"); struct anode *root=(struct anode*)malloc(sizeof(struct anode)); root=NULL; while(1) { printf("\n1. Insert\n"); printf("\n2. Display\n"); printf("\n3. Return to tree menu\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the value to be inserted:"); scanf("%d",&info); if(avlsearch(root,info)==NULL) root=avlinsert(info,root,&ht_inc); else printf("\n Duplicate value ignored\n"); break; case 2: if(root=NULL) { printf("\n Tree is empty\n"); continue; } printf("\n Tree is:\n"); avldisplay(root,1); printf("\n\n"); printf("\n Inorder traversal is:"); avlinorder(root); printf("\n"); break; case 3: return; default : printf("\nWrong choice\n"); }//end of switch }//end of while }//end of main struct anode *avlsearch(struct anode*ptr,int info) { if(ptr!=NULL) if(info<ptr->info) ptr=avlsearch(ptr->lchild,info); else if(info>ptr->info) ptr=avlsearch(ptr->rchild,info); return ptr; }//end of search struct anode *avlinsert(int info,struct anode*pptr,booleanx*ht_inc) { struct anode *aptr; struct anode *bptr; if(pptr==NULL) { pptr=(struct anode*)malloc(sizeof(struct anode)); pptr->info=info; pptr->lchild=NULL; pptr->rchild=NULL; pptr->balance=0 *ht_inc=true; return pptr; } if (info<pptr->info) { pptr->lchild=avlinsert(info,pptr->lchild,ht_inc); if(*ht_inc==true) { switch(pptr->balance) { case 1://right heavy; pptr->balance=0 *ht_inc=false; break; case 0: //balanced pptr->balance=1; break; case -1://left heavy aptr=pptr->lchild; if(aptr->balance==-1) { printf("left to left rotation\n"); pptr->lchild=aptr->rchild; aptr->rchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("left to right rotation\n"); bptr=aptr->rchild; aptr->rchild=bptr->lchild; bptr->lchild=aptr; pptr->lchild=bptr->rchild; bptr->rchild=pptr; if (bptr->balance==1) pptr->balance=-1; else pptr->balance=0; if(bptr->balance==-1) aptr->balance=1; else aptr->balance=0; bptr->balance=0; pptr=bptr; } *ht_inc=false; }//end of switch }//end of if }//end of if if(info>pptr->info) { pptr->rchild=avlinsert(info,pptr->rchild,ht_inc); if (*ht_inc==true) { switch (pptr->balance) { case 1://left heavy pptr->balance=0; *ht_inc=false; break; case 0://balanced pptr->balance=-1; break; case -1: //right heavy aptr=pptr->rchild; if (aptr->balance==-1) { printf("Right to right rotation\n"); pptr->rchild=aptr->lchild; aptr->lchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("Right to left rotation\n"); bptr=aptr->lchild; aptr->lchild=bptr->rchild; bptr->rchild=aptr; pptr->rchild=bptr->lchild; bptr->lchild=pptr; if(bptr->balance==-1) pptr->balance=1; else pptr->balance=0; if(bptr->balance==1) aptr->balance=-1; else aptr->balance=0; bptr->balance=0; pptr=bptr; }//end of else *ht_inc=false; }//end of switch }//end of if }//end of if return pptr; }//end of the function void avldisplay(struct anode*ptr,int level) { int i; if (ptr!=NULL) { avldisplay(ptr->rchild,level+1); printf("\n"); for (i=0; i<level;i++ ) printf(" "): printf("%d",ptr->info); avldisplay(ptr->lchild,level+1); }//end of if }//end of function void avlinorder(struct anode*ptr) { if (ptr!=NULL) { avlinorder(ptr->lchild); printf("%d",ptr->info); avlinorder(ptr->rchild); } }