‹ Back to the paper
[Programme based on Strings] Two words are called anagram of each other if both the words contain…
[Programme based on Strings]
Two words are called anagram of each other if both the words contain the same letters but they are arranged in different order. Following are the three pairs of the words which are anagrams of each other.
cat, act heart, earth silent, listen
Design the class Anagram to input two words in lower case and check if they are anagram of each other. The member functions and data members of the class are given below:
Class Name : Anagram
Data members/instance variables:
s1, s2 : stores the two words in lower case
l1, l2 : number of character in s1 and s2 respectively
Member functions :
Anagram(String s1, String s2) : parameterised constructor to assign the accepted values of s1 and s2 to data member
void sort(char c[]) : to sort the characters in array c[ ] in ascending order using any sorting technique
boolean areAnagram(char[] str1, char[] str2) : to check if str1 and str2 are anagrams of each other and to return true or false accordingly
void check() : to convert the two strings into two character arrays and to display the appropriate message as shown in the sample output by calling the method areAnagram(char[] str1, char[] str2)
Specify the class Anagram giving details of the parameterised constructor, void sort(char c[ ]), boolean areAnagram(char[] str1, char[] str2) and void check( ). Create one object in the main( ) method and call all the methods appropriately.
Sample input
Enter first word->earth
Enter second word->heart
Output
earth and heart are anagram of each other
Sample input/output
Sample input
Enter first word->earth
Enter second word->heart
Output
earth and heart are anagram of each otherAnswer
Answer
AIimport java.util.Scanner;
class Anagram
{
String s1, s2; // the two words in lower case
int l1, l2; // number of characters in s1 and s2
// parameterised constructor
Anagram(String s1, String s2)
{
this.s1 = s1;
this.s2 = s2;
l1 = s1.length();
l2 = s2.length();
}
// sorts the characters of c[] in ascending order using bubble sort
void sort(char c[])
{
int n = c.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (c[j] > c[j + 1])
{
char t = c[j];
c[j] = c[j + 1];
c[j + 1] = t;
}
}
// checks if str1 and str2 are anagrams by sorting both and comparing
boolean areAnagram(char[] str1, char[] str2)
{
if (str1.length != str2.length)
return false;
sort(str1);
sort(str2);
for (int i = 0; i < str1.length; i++)
if (str1[i] != str2[i])
return false;
return true;
}
// converts s1 and s2 to character arrays and displays the result
void check()
{
char c1[] = s1.toCharArray();
char c2[] = s2.toCharArray();
if (areAnagram(c1, c2))
System.out.println(s1 + " and " + s2 + " are anagram of each other");
else
System.out.println(s1 + " and " + s2 + " are not anagram of each other");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first word->");
String s1 = sc.next();
System.out.print("Enter second word->");
String s2 = sc.next();
Anagram ob = new Anagram(s1, s2);
ob.check();
}
}Explanation: sort() rearranges a character array into ascending order with bubble sort; areAnagram() sorts copies of both words and returns true only if every position then matches (same length and same multiset of letters). check() converts the two stored words to char arrays and prints the appropriate message. Tested (run for real): "earth"/"heart" printed "earth and heart are anagram of each other" (matching the sample), "silent"/"listen" also correctly reported as anagrams, and "hello"/"world" was correctly reported as not anagrams.From ISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024), question 72.