diff --git a/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java b/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java index 171edca..c0d01be 100644 --- a/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java +++ b/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java @@ -1,29 +1,26 @@ import java.util.Scanner; +import java.util.Arrays; + class palin_alpha_rev_concat{ static boolean palindrome(String str) { String rev=reverse(str); return str.contentEquals(rev); } - static String[] alphabetic(String str) - { - String arr[]=str.split(" "); - for(int i=0;i0) - { - String temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - return arr; + static String[] alphabetic(String str) { + char[] arr = str.toCharArray(); + Arrays.sort(arr); + String sortedStr = new String(arr); + + String[] result = new String[sortedStr.length()]; + for (int i = 0; i < sortedStr.length(); i++) { + result[i] = String.valueOf(sortedStr.charAt(i)); + } + + return result; } + static String reverse(String str) { StringBuffer rev=new StringBuffer(str); @@ -52,13 +49,11 @@ class palin_alpha_rev_concat{ System.out.println("String is a palindrome:"+palindrome(str)); break; } - case 2: - { - String arr[]=alphabetic(str); + case 2: { + String arr[] = alphabetic(str); System.out.println("String in alphabetic order:"); - for(int i=0;i