Total members 11806 |It is currently Thu Nov 21, 2019 4:44 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

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.




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


Author:
Mastermind
User avatar Posts: 2715
Have 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;
  }



Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time
Post new topic Reply to topic  [ 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 Algorithms, Java Strings






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