From c6399a82f43d94123a3ae254025e8a6177f2bc54 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 14 Oct 2024 11:07:40 +0530 Subject: [PATCH] Update OOP/Java/Lab/Week9/palin_alpha_rev_concat.java --- .../Lab/Week9/palin_alpha_rev_concat.java | 80 ++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java b/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java index c0d01be..8cf4f8f 100644 --- a/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java +++ b/OOP/Java/Lab/Week9/palin_alpha_rev_concat.java @@ -28,7 +28,7 @@ class palin_alpha_rev_concat{ String revstr=rev.toString(); return revstr; } - static String concating(String str) + static String concatenate(String str) { String rev=reverse(str); return str+rev; @@ -65,7 +65,83 @@ class palin_alpha_rev_concat{ } case 4: { - System.out.println("Concatenated String="+concating(str)); + System.out.println("Concatenated String="+concatenate(str)); + break; + } + } + System.out.println("Continue?(y/n)"); + contin=in.next().charAt(0); + } + + } +}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) { + 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); + rev.reverse(); + String revstr=rev.toString(); + return revstr; + } + static String concatenate(String str) + { + String rev=reverse(str); + return str+rev; + } + + public static void main(String[] args) { + Scanner in=new Scanner(System.in); + System.out.println("Enter the String"); + String str=in.nextLine(); + char contin='y'; + while(contin=='y'){ + System.out.println("What do you want to do? 1. Check for Palindrome; 2. Write in an alphabetic order; 3. Reverse the String; 4. Concatenate with reversed"); + int choice=in.nextInt(); + switch(choice) + { + case 1: + { + System.out.println("String is a palindrome:"+palindrome(str)); + break; + } + case 2: { + String arr[] = alphabetic(str); + System.out.println("String in alphabetic order:"); + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i]); + } + System.out.println(); + break; + } + case 3: + { + System.out.println("Reversed string="+reverse(str)); + break; + } + case 4: + { + System.out.println("Concatenated String="+concatenate(str)); break; } }