Saturday 13 December 2014

Given a string as input, return the following output create("1234") = "1234 234 34 4" create("ABCAB") = "ABCAB BCAB CAB AB B" create("ABC") = "ABC BC C" create("") = "" create("X") = "X" create("XY") = "XY Y"

public class ReplaceOneAndPrint {
  static String testcase="ABC ";
    public static void main(String args[]){
        ReplaceOneAndPrint testInstance=new ReplaceOneAndPrint();
        String result=testInstance.checkString(testcase);
        System.out.println(result);
       
    }
    public String checkString(String str){
        String holdReplaceFormat=str;//store the max occurence of char in string
        String newString="";
        String oneWhiteSpace=" ";
        char holdEachChar=' ';//hold each char of the string
      
        int i=0;
        int lenStr=str.length();
        int lenholdReplaceFormat=0;
      
        while(lenStr>0){//check the string len coz each time the string getting shorter
            holdEachChar=holdReplaceFormat.charAt(i);//hold the first char of string each time
          

           holdReplaceFormat=holdReplaceFormat.substring(i+1);
          
            newString = newString+oneWhiteSpace+holdReplaceFormat;
            System.out.println(newString);
           lenholdReplaceFormat=holdReplaceFormat.length();
           lenStr=lenholdReplaceFormat;
            System.out.println("lenStr issss:"+lenStr);
            if(lenStr==1){
                break;
            }
        } 
        newString=str+newString;
    return newString;       
    }       
}
   

No comments:

Post a Comment