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