Update OOP/Java/Lab/Week9/palin_alpha_rev_concat.java

This commit is contained in:
Aadit Agrawal 2024-10-14 09:46:21 +05:30
parent c910a492db
commit 0bdd6abbbc

View File

@ -1,82 +1,82 @@
import java.util.Scanner; import java.util.Scanner;
class palin_alpha_rev_concat{ class palin_alpha_rev_concat{
static boolean palindrome(String str) static boolean palindrome(String str)
{ {
String rev=reverse(str); String rev=reverse(str);
return str.contentEquals(rev); return str.contentEquals(rev);
} }
static String[] alphabetic(String str) static String[] alphabetic(String str)
{ {
String arr[]=str.split(" "); String arr[]=str.split(" ");
for(int i=0;i<arr.length-1;i++) for(int i=0;i<arr.length-1;i++)
{ {
for(int j=0;j<arr.length-i-1;j++) for(int j=0;j<arr.length-i-1;j++)
{ {
int a=arr[j].compareTo(arr[j+1]); int a=arr[j].compareTo(arr[j+1]);
if(a>0) if(a>0)
{ {
String temp=arr[j]; String temp=arr[j];
arr[j]=arr[j+1]; arr[j]=arr[j+1];
arr[j+1]=temp; arr[j+1]=temp;
} }
} }
} }
return arr; return arr;
} }
static String reverse(String str) static String reverse(String str)
{ {
StringBuffer rev=new StringBuffer(str); StringBuffer rev=new StringBuffer(str);
rev.reverse(); rev.reverse();
String revstr=rev.toString(); String revstr=rev.toString();
return revstr; return revstr;
} }
static String concating(String str) static String concating(String str)
{ {
String rev=reverse(str); String rev=reverse(str);
return str+rev; return str+rev;
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner in=new Scanner(System.in); Scanner in=new Scanner(System.in);
System.out.println("Enter the String"); System.out.println("Enter the String");
String str=in.nextLine(); String str=in.nextLine();
char contin='y'; char contin='y';
while(contin=='y'){ while(contin=='y'){
System.out.println("What do you want to do?1 for checking palindrome,2 for alphabetic,3 for reversing,4 for concatinatig with reverse"); 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(); int choice=in.nextInt();
switch(choice) switch(choice)
{ {
case 1: case 1:
{ {
System.out.println("String is a palindrome:"+palindrome(str)); System.out.println("String is a palindrome:"+palindrome(str));
break; break;
} }
case 2: case 2:
{ {
String arr[]=alphabetic(str); String arr[]=alphabetic(str);
System.out.println("String in alphabetic order:"); System.out.println("String in alphabetic order:");
for(int i=0;i<arr.length;i++) for(int i=0;i<arr.length;i++)
{ {
System.out.print(arr[i]+" "); System.out.print(arr[i]+" ");
} }
System.out.println(); System.out.println();
break; break;
} }
case 3: case 3:
{ {
System.out.println("Reversed string="+reverse(str)); System.out.println("Reversed string="+reverse(str));
break; break;
} }
case 4: case 4:
{ {
System.out.println("Concatenated String="+concating(str)); System.out.println("Concatenated String="+concating(str));
break; break;
} }
} }
System.out.println("Continue?(y/n)"); System.out.println("Continue?(y/n)");
contin=in.next().charAt(0); contin=in.next().charAt(0);
} }
} }
} }