Sunday, August 26, 2012

How to Reverse a String


public class ReverseString {
   
   
    public static void main(String st[]){
       
        String s = "12345678";
        char a[] = s.toCharArray();
        int l = s.length()-1;
        char temp;
        int i = 0;
        while(i <= l/2){
           
            temp = a[i];
            a[i] = a[l-i];
            a[l-i] = temp;
            i++;
           
        }
        System.out.println(a);
    }

}

Friday, August 24, 2012

how to convert decimal numbers to roman numerals


public class LetterToRoman {

    private static final String[] RCODE = {"M", "CM", "D", "CD", "C", "XC", "L",
        "XL", "X", "IX", "V", "IV", "I"};
private static final int[]    BVAL  = {1000, 900, 500, 400,  100,   90,  50,
        40,   10,    9,   5,   4,    1};

public static void main(String st[]){
   
    int num = 11;
    int i = 0;
    int gnum = num;
    while (gnum!=0){
       
        while(!((BVAL[i]<=gnum) )){
            i++;
        }
            System.out.print(RCODE[i]);
            gnum = gnum - BVAL[i];
       
    }
}
}

Wednesday, August 22, 2012

Write a program which returns true if the given string contains the consecutive repeated substring .Ex-adabcabcd here abc is consecutive repeated substring.

import java.util.ArrayList;
import java.util.List;


public class Consecutiverepeat {

  
    static String s = "daradarabcece";
    static char[] ch = s.toCharArray();
  
    public static void main(String str[]){
      
        int i=1;
        while(i<ch.length){
            List<Integer> ls = isInPastArray(i);
            if(ls.size()!=0){
                for(Integer intg: ls){
                    int leg = i-intg;
                    System.out.println(leg);
                  
                    if((i+leg-1 <ch.length )&&(ch[i-1]==ch[i+leg-1])){
                        if(compare(i, leg)){
                            System.out.println("true");
                            return;
                        }
                    }                  
                }
            }
            i++;
        }
    }
    static boolean compare(int i,int len){
      
        int s1 = i;
        int s2=i-len;
        while(len>=1){
            System.out.println("ch["+s1+"]"+ch[s1]+"  ch["+s2+"]"+ch[s2]);
            if(ch[s1] != ch[s2])
                return false;
        s1++;
        s2++;
        len--;
        }
      
      
        return true;
      
    }
  
    static List<Integer> isInPastArray(int p){
      
        char ch1 = ch[p];
        List <Integer> list = new ArrayList<Integer>();
        int i =0;
        int pi = 0;
        while(i<p){
            if(ch[i] == ch1){
                list.add(i);
            }
            i++;
        }
      
        return list;
    }
}