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();
}
}
//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);
}
}
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
3,575 views
Usually answered in minutes!
What anagrams can you come up with for SAINT PATRICKS DAY.
SAINT PATRICKS DAY anagrams
×