Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:38 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 3 posts ] 
Author Question
 Question subject: anagram in java
PostPosted: Mon Sep 05, 2011 6:45 am 

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.


TOP
  
Reply with quote  
 Question subject: Re: anagram in java
PostPosted: Thu Sep 08, 2011 11:49 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
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);
        }

    }
}

 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: anagram in java
PostPosted: Fri Sep 16, 2011 1:35 pm 
Offline
Newbie
User avatar

Joined: Mon Jul 30, 2007 1:11 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 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;
  }


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 3 posts ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team