From e0f55e7fd5e66dc47cf13402ac33186d62aa12e6 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 14 Oct 2024 09:56:35 +0530 Subject: [PATCH] Update OOP/Java/Lab/Week9/palin_alpha_rev_concat.java --- .../Lab/Week9/palin_alpha_rev_concat.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) 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