Total members 11835 |It is currently Sun Sep 27, 2020 5:56 am Login / Join Codemiles

### Weka

Write a program to find if the given two strings are anagram are not. An anagram of a string is a string obtained by permuting the letters of a string. For example aaba and aaab are anagrams, while abcd and deba are not.

 Tweet
Author:

Code:

import java
.io.IOException;

public class
ExampleClass {

static
int sizeOne;
static
int count;
static
char[] firstCharArray;
static
char[] secondCharArray;

public static
void main(String[] argsthrows IOException {

String firstString "Java";

String secondString "Jvaa";

sizeOne firstString.length();

count 0;

firstCharArray = new char[sizeOne];

secondCharArray = new char[sizeOne];

// assumption both are equal in size.

for (int j 0sizeOnej++) {

firstCharArray[j] = firstString.charAt(j);

secondCharArray[j] = secondString.charAt(j);
}

startAnagram(sizeOne);

System.out.println("The strings are not anagram ");
}

// recursive anagram algorithem

public static void startAnagram(int stringSize) {

if (
stringSize == 1) {
return;
}

for (
int i 0stringSizei++) {

startAnagram(stringSize 1);
if (
stringSize == 2// if innermost,

{
print();
}

rotateAnagram(stringSize); // rotate word

}
}

// rotate left all chars from position to end

public static void rotateAnagram(int currentSize) {

int i;

int position sizeOne currentSize;

// temp for current letter.

char temp firstCharArray[position];

//shift others left

for (position 1sizeOnei++) {

firstCharArray[1] = firstCharArray[i];
}

//put first on right

firstCharArray[1] = temp;
}

public static
void print() {

for (
int i 0sizeOnei++) {

System.out.print(firstCharArray[i]);
}

System.out.println("");

checkIfStringsEqual();
}

public static
void checkIfStringsEqual() {

boolean flag true;

for (
int i 0sizeOnei++) {
if (
firstCharArray[i] != secondCharArray[i]) {

flag false;
break;
}
}
if (
flag) {

System.out.println("The strings are anagram ");

System.exit(1);
}

}
}

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada

 Tweet
Author:
 Posts: 2715Have thanks: 74 time

Short version:
Code:
public static void main(String[] arg) {
String str1 = "hello";
String str2 = "olleh";

if (isAnagram(str1, str2)) {
System.out.println("Anagram");
} else {
System.out.println("Not Anagram");
}
}

private static boolean isAnagram(String str1, String str2) {
if (str1.length() != str2.length()) return false;
for (int i = 0; j = str2.length() -1; i <= j; i++, j--) {
if (str1.charAt(i) != str2.charAt(j)) {
return false;
}
}
return true;
}

 Tweet
Author:
 Posts: 3Have thanks: 0 time
 Page 1 of 1 [ 3 posts ]

Related Posts  to : anagram in java
Java Anagram     -
2d game in java-Monster-Java 2D Game Graphics and Animation     -
need help in java     -
What is Java API?!!!     -
java or .net     -
Using FTP in java     -
what is java     -
Java course     -
Draw An Arc in java     -
java Expressions     -

### Topic Tags

Java Strings, Java Algorithms

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com