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