Computers & Internet Logo

Related Topics:

Posted on Nov 22, 2010

A java code for anagram - Computers & Internet

  • Anonymous Mar 16, 2014

    What anagrams can you come up with for SAINT PATRICKS DAY.

  • Anonymous Mar 16, 2014

    SAINT PATRICKS DAY anagrams

×

3 Answers

Anonymous

Level 1:

An expert who has achieved level 1.

MVP:

An expert that got 5 achievements.

New Friend:

An expert that has 1 follower.

Hot-Shot:

An expert who has answered 20 questions.

  • Contributor 24 Answers
  • Posted on Nov 25, 2010
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

MVP:

An expert that got 5 achievements.

New Friend:

An expert that has 1 follower.

Hot-Shot:

An expert who has answered 20 questions.

Joined: Mar 30, 2010
Answers
24
Questions
0
Helped
11239
Points
43

Import java.io.IOException;

public class AnagramApp {
static int size;

static int count;

static char[] charArray;

public static void main(String[] args) throws IOException {
String input = "Java Source and Support";
size = input.length();
count = 0;
charArray = new char[size];
for (int j = 0; j < size; j++)
charArray[j] = input.charAt(j);
doAnagram(size);
}

public static void doAnagram(int newSize) {
int limit;
if (newSize == 1) // if too small, return;
return;
// for each position,
for (int i = 0; i < newSize; i++) {
doAnagram(newSize - 1); // anagram remaining
if (newSize == 2) // if innermost,
display();
rotate(newSize); // rotate word
}
}

// rotate left all chars from position to end
public static void rotate(int newSize) {
int i;
int position = size - newSize;
// save first letter
char temp = charArray[position];
//shift others left
for (i = position + 1; i < size; i++)
charArray[i - 1] = charArray[i];
//put first on right
charArray[i - 1] = temp;
}

public static void display() {
System.out.print(++count + " ");
for (int i = 0; i < size; i++)
System.out.print(charArray[i]);
System.out.println();
}
}

Anonymous

Level 1:

An expert who has achieved level 1.

  • Contributor 1 Answer
  • Posted on Aug 22, 2012
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

Joined: Aug 22, 2012
Answers
1
Questions
0
Helped
3565
Points
0

//programmer: Nguyen Phan
//program: Write a method that checks whether two words are anagrams.
import java.util.Scanner;
public class Anagrams {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);
System.out.println("Enter your first string: ");
String string1 = input.nextLine();
System.out.println("Enter your second string: ");
String string2 = input.nextLine();



if (isAnagram(sort(string1), sort(string2)) == true){
System.out.println("Anagrams");

}else{
System.out.println("not Anagrams");
}
System.out.println(string1);
System.out.println(string2);

}

public static String sort(String s){
char[] s1 = new char[s.length()];
s1 = s.toCharArray();

java.util.Arrays.sort(s1);
s = new String(s1);
return s;
}

public static boolean isAnagram(String s1, String s2){
return s1.equals(s2);
}

}

Ad

Anonymous

Level 1:

An expert who has achieved level 1.

New Friend:

An expert that has 1 follower.

  • Contributor 1 Answer
  • Posted on Apr 29, 2011
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

New Friend:

An expert that has 1 follower.

Joined: Apr 29, 2011
Answers
1
Questions
1
Helped
3565
Points
1

Miderm Exam

Create a class named Midterm that contains two methods

(Anagrams) Write a method that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order. For example, "silent" and "listen" are anagrams. The header of the method is as follows:
public static boolean isAnagram(String s1, String s2)


(Sorting characters in a string) Write a method that returns a sorted string using the following header:
public static String sort(String s)
For example, sort("acb") returns abc.

In the main method, invoke isAnagram("silent", "listen"), isAnagram("garden", "ranged"), and isAnagram("split", "lisp").
In the main methods invoke sort("acb"), sort("123def")

try it out as well with other values

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

3,575 views

Ask a Question

Usually answered in minutes!

Top Computers & Internet Experts

ADMIN Andrew
ADMIN Andrew

Level 3 Expert

73783 Answers

ADMIN Eric
ADMIN Eric

Level 3 Expert

42222 Answers

Brad Brown

Level 3 Expert

19190 Answers

Are you a Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...