Pages

Wednesday 11 April 2012


Print continuous alphabets from a sequence of arbitrary alphabets
For example:
Input: abcdefljdflsjflmnopflsjflasjftuvwxyz
Output: abcdef; mnop; tuvwxyz
Input: AbcDefljdflsjflmnopflsjflasjftuvWxYz
Output: abcdef; mnop; tuvwxyz



public class ContinuousAlphabets {
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        System.out.println(str);
       
        if(str==null || str.length()<=1)
            return;
        str = str.toLowerCase();
        StringBuffer temp = new StringBuffer();
        for(int i =0;i<str.length();i++){
            if((temp.length()==0) || ((temp.length()>0) && (str.charAt(i) - temp.charAt(temp.length()-1) == 1)))
                temp.append(str.charAt(i));
            else{
                if(temp.length()>1){
                    System.out.println(temp);
                }
                temp.delete(0, temp.length());
                temp.append(str.charAt(i));
            }   
        }
        if(temp.length()>1){
            System.out.println(temp);
        }
    }
  
}

No comments:

Post a Comment