package org.Solutions;
import java.util.Arrays;
public class StringRoutines {
void bubbleSort(char[] chrArray){
int arrLength= chrArray.length;
int k=0;
char tmp;
for(int m=arrLength;m>0;m--){
for(int i=0;i<arrLength-1;i++){
k=i+1;
if(chrArray[i] > chrArray[k]){
tmp = chrArray[k];
chrArray[k]=chrArray[i];
chrArray[i]=tmp;
}
}
}
}
int arrLength= chrArray.length;
int k=0;
char tmp;
for(int m=arrLength;m>0;m--){
for(int i=0;i<arrLength-1;i++){
k=i+1;
if(chrArray[i] > chrArray[k]){
tmp = chrArray[k];
chrArray[k]=chrArray[i];
chrArray[i]=tmp;
}
}
}
}
String sortStringCharacters(String str){
char[] chr=new char[str.length()];
chr=str.toCharArray();
bubbleSort(chr);
str="";
for(int i=0;i<chr.length;i++)
str=str+chr[i];
return str;
}
char[] chr=new char[str.length()];
chr=str.toCharArray();
bubbleSort(chr);
str="";
for(int i=0;i<chr.length;i++)
str=str+chr[i];
return str;
}
public boolean isAnagaram(String str1,String str2){
boolean anag_Result=false;
boolean anag_Result=false;
if(str1 != null && str2 != null && str1.length()==str2.length()){
str1 = sortStringCharacters(str1);
str2 = sortStringCharacters(str2);
return str1.equals(str2);
}else{
anag_Result = false;
}
str1 = sortStringCharacters(str1);
str2 = sortStringCharacters(str2);
return str1.equals(str2);
}else{
anag_Result = false;
}
return anag_Result;
}
}
Main Class:
-------------------
-------------------
package org.Solutions;
public class MainClass {
public static void main(String[] args) {
StringRoutines sr=new StringRoutines();
String str1="Killer Rat";
String str2="Rat Killer";
if(sr.isAnagaram(str1,str2 )){
System.out.println("Str 1: "+str1+" Str2:"+str2);
System.out.println("Given Strings are Anagram");
}else{
System.out.println("Given Strings are not Anagram");
}
}
StringRoutines sr=new StringRoutines();
String str1="Killer Rat";
String str2="Rat Killer";
if(sr.isAnagaram(str1,str2 )){
System.out.println("Str 1: "+str1+" Str2:"+str2);
System.out.println("Given Strings are Anagram");
}else{
System.out.println("Given Strings are not Anagram");
}
}
}
Output:
---------------------
---------------------
Str 1: Killer Rat Str2:Rat Killer
Given Strings are Anagram
Given Strings are Anagram
Comments
Post a Comment