Friday 6 February 2015

Socket Programming with Multiple Thread

JSERVER.JAVA import java.io.*; import java.net.*; class JServer { public static void main(String[] args) { try{ ServerSocket ss=new ServerSocket(5700); while(true){ Socket cs=ss.accept(); Worker w=new Worker(cs); Thread t1=new Thread(w); t1.start(); } } catch(Exception e) {System.out.println(e.getMessage());} } } WORKER.JAVA import java.io.*; import java.net.*; class Worker implements Runnable { Socket s; Worker(Socket s) { this.s=s; } public void run() { try{ InputStreamReader isr=new InputStreamReader(s.getInputStream()); OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream()); BufferedReader br=new BufferedReader(isr); BufferedWriter bw=new BufferedWriter(osw); while(true){ String input=br.readLine(); bw.write("KMIT"+input); bw.newLine(); bw.flush(); } }catch(Exception e) {System.out.println(e.getMessage());} } } JCLIENT.JAVA import java.io.*; import java.net.*; class JClient { public static void main(String[] args) { try{ Socket s=new Socket("localhost",5700); InputStreamReader isr=new InputStreamReader(s.getInputStream()); OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream()); BufferedReader br=new BufferedReader(isr); BufferedWriter bw=new BufferedWriter(osw); while(true) { bw.write("HYDERABAD"); bw.newLine(); bw.flush(); String output=br.readLine(); System.out.println(output); } } catch(Exception e) {System.out.println(e.getMessage());} } }

Given two strings a and b, find whether any anagram of string a is a sub-string of string b

if a = xyz and b = afdgzyxksldfm then the program should return true. class StringDemo3 { static boolean checkAnagram(String s1,String s2) { int mycount[]=new int[128]; for (int i=0;i<s1.length();i++) { mycount[s1.charAt(i)]++; mycount[s2.charAt(i)]--; } for (int i=0;i<127;i++) { if(mycount[i]!=0) return false; } return true; } public static void main(String[] args) { String target="xyz"; String source="afdgzyxksldfm"; boolean flag=false; for (int i=0;i<source.length()-target.length()+1;i++) { if(checkAnagram(target,source.substring(i,i+target.length()))) { System.out.println("Anagram Found"); flag=true; break; } } if(!flag) System.out.println("Anagram Not Found"); } }

Code a function that receives a string composed by words separated by spaces and returns a string where words appear in the same order but than the original string, but every word is inverted

Example, for this input string "the boy ran" the output would be "eht yob nar" Tell the complexity of the solution class StringDemo2 { static String myRevers(String s) { StringBuilder sb=new StringBuilder(); for (int i=s.length()-1;i>=0;i-- ) { sb.append(s.charAt(i)); } return sb.toString(); } public static void main(String[] args) { String x="the boy ran"; } }

Write a program to expand a given string x to y?

input string x = "1..5,8,11..14,18,20,26..29" output string y = "1,2,3,4,5,8,11,12,13,14,18,20,26,27,28,29" import java.util.*; class StringDemo1 { public static void main(String[] args) { String x="1..5,8,11..14,18,20,26..29"; String sarr[]=x.split(","); StringBuilder sb=new StringBuilder(); for (String s:sarr) { if(s.indexOf('.')!=-1) { String limits[]=s.split("\\.\\."); int llimit=Integer.parseInt(limits[0]); int ulimit=Integer.parseInt(limits[1]); for (int i=llimit;i<=ulimit;i++) { sb.append(""+i+","); } } else sb.append(s+","); } System.out.println(sb); } }

pattern 5

1 2*3 4*5*6 7*8*9*10 7*8*9*10 4*5*6 2*3 1 #include <stdio.h> void draw_line(int n); void draw_pattern(int n,int m); void draw_rpattern(int n,int m); void main() { } void draw_line(int n) { int i; for (i=0;i<n-1;i++ ) { printf("%d*",n); } printf("%d\n",n); } void draw_pattern(int n,int m) { if(n==1) draw_line(n); else { draw_pattern(n-1,m); draw_line(n); if(n==m) draw_rpattern(n,m); } } void draw_rpattern(int n,int m) { if(n==1) draw_line(n); else { draw_line(n); draw_rpattern(n-1,m); } }

pattern 4

1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1 #include <stdio.h> void draw_line(int n); void draw_pattern(int n,int m); void draw_rpattern(int n,int m); void main() { draw_pattern(4,4); } void draw_line(int n) { int i; for (i=0;i<n-1;i++ ) { printf("%d*",n); } printf("%d\n",n); } void draw_pattern(int n,int m) { if(n==1) draw_line(n); else { draw_pattern(n-1,m); draw_line(n); if(n==m) draw_rpattern(n,m); } } void draw_rpattern(int n,int m) { if(n==1) draw_line(n); else { draw_line(n); draw_rpattern(n-1,m); } }

pattern 3

if n=7 * * * * * * * * * * * * * * * * * * * * * * * * * #include <stdio.h> void draw_line(int nst,int n) { int i; int nsp=(n-nst)/2; for (i=0;i<nsp;i++) printf(" "); for (i=0;i<nst;i++) printf("*"); printf("\n"); } void draw_rpattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_line(nst,n); draw_rpattern(nst-2,n); } } void draw_pattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_pattern(nst-2,n); draw_line(nst,n); if(nst==n) draw_rpattern(nst-2,n); } } void main() { draw_pattern(7,7); }

pattern 2 program

if n=7 * * * * * * * * * * * * * * * * #include <stdio.h> void draw_line(int nst,int n) { int i; int nsp=(n-nst)/2; for (i=0;i<nsp;i++) printf(" "); for (i=0;i<nst;i++) printf("*"); printf("\n"); } void draw_pattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_pattern(nst-2,n); draw_line(nst,n); } } void main() { draw_pattern(7,7); }

pattern program 1

1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 #include <stdio.h> int draw_line(int flag,int val,int count) { int i,k; if(!flag) { for (i=0,k=val ;i< count-1;i++ ) { printf("%d*",k); k++; } printf("%d\n",k); return k; } else { int hval=val+count-1; for (i=0,k=hval;i<count-1;i++,k--) { printf("%d*",k); } printf("%d\n",k); return hval; } } int draw_pattern(int n) { int r; if(n==1) { printf("%d\n",n); return n; } r=draw_pattern(n-1); if(n%2) return draw_line(0,r+1,n); else return draw_line(1,r+1,n); } void main() { draw_pattern(6); }

Create a Single Linked List program using C

#include struct Node { int data; struct Node *next; }; struct Node *create_list(int *ptr,int n) { struct Node *hptr=NULL,*tptr,*temp; int i; if(n>0) { hptr=(struct Node*)malloc(sizeof(struct Node)); tptr=hptr; hptr->data=ptr[0]; for (i=1;idata=ptr[i]; temp->next=NULL; tptr->next=temp; tptr=temp; } } return hptr; } void display(struct Node *dptr) { struct Node *temp; for (temp=dptr;temp!=NULL ;temp=temp->next ) { printf("%d\n",temp->data); } } struct Node *getNfromLast(struct Node *hptr,int n) { struct Node *fptr,*bptr; int i; fptr=hptr; bptr=hptr; for (i=0;inext; while(fptr->next!=NULL) { fptr=fptr->next; bptr=bptr->next; } return bptr; } void main() { int arr[]={1,2,3,4,5,6}; struct Node *hptr,*tptr; hptr=create_list(arr,6); display(hptr); tptr=getNfromLast(hptr,3); printf("\n%d\n",tptr->data); }

Create a Single Linked List using array and find an element in the list

#include <stdio.h> struct Node { int data; struct Node *next; }; struct Node *create_list(int *ptr,int n) { struct Node *hptr=NULL,*tptr,*temp; int i; if(n>0) { hptr=(struct Node*)malloc(sizeof(struct Node)); tptr=hptr; hptr->data=ptr[0]; for (i=1;i<n;i++) { temp=(struct Node *)malloc(sizeof(struct Node)); temp->data=ptr[i]; temp->next=NULL; tptr->next=temp; tptr=temp; } } return hptr; } char* rfind(struct Node *hptr,int d) { if(hptr==NULL) return "NOT FOUND"; else if(hptr->data==d) return "FOUND"; else return rfind(hptr->next,d); } void main() { int arr[]={10,12,53,84,35,16}; struct Node *hptr,*tptr; hptr=create_list(arr,6); printf("%s\n",rfind(hptr,45)); }

Find count the number of occurrence of Strings in a Sentence?

Hint: input:this is String this is String output:[this 2 is 2 String 2] import java.util.*; public class FC { public static void main(String[] args) { HashMap<String,Integer> hm=new HashMap<String,Integer>(); for(String s:args) { if(hm.get(s)!=null) { int v=hm.get(s); v=v+1; hm.put(s,v); } else hm.put(s,1); } System.out.println(hm); } }

Thursday 29 January 2015

Find the Binary Or in Strings using Java

public class BinaryOr { public static void main(String[] args) { String str1="0101"; String str2="1011"; int c=0; StringBuilder sb=new StringBuilder(); for (int i=0;i<str1.length();i++) { int fchar=str1.charAt(str1.length()-1-i)-'0'; int schar=str2.charAt(str2.length()-1-i)-'0'; int sum=fchar+schar+c; int rbit=sum%2; c=sum>>1; sb.insert(0,(char)(rbit+'0')); } if(c==1) sb.insert(0,(char)'1'); System.out.println(sb.toString()); } }

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)); }