Computer Applications
Define a class Anagram to accept two words from the user and check whether they are anagram of each other or not.
An anagram of a word is another word that contains the same characters, only the order of characters is different.
For example, NOTE and TONE are anagram of each other.
Answer
import java.util.Scanner;
public class Anagram
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the 2 words to check");
System.out.print("Enter first word: ");
String str1 = in.next();
System.out.print("Enter second word: ");
String str2 = in.next();
boolean isAnagram = true;
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
int l1 = s1.length();
int l2 = s2.length();
if (l1 != l2) {
isAnagram = false;
}
else {
int count[] = new int[26];
for (int i = 0; i < l1; i++) {
count[s1.charAt(i) - 97]++;
count[s2.charAt(i) - 97]--;
}
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
isAnagram = false;
break;
}
}
}
if (isAnagram)
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
Output
Related Questions
Define a class to accept a number and check whether the number is valid number or not. A valid number is a number in which the eventual sum of digits of the number is equal to 1.
e.g., 352 = 3 + 5 + 2 = 10 = 1 + 0 = 1
Then 352 is a valid number.
Write the code to print following patterns
(i)
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
(ii)
A A A A A A A A B B A A C C C A D D D D E E E E E
Define a class to accept the elements of an array from the user and check for the occurrence of positive number, negative number and zero.
Example
Input Array: 12, -89, -56, 0, 45, 56
Output:
3 Positive Numbers
2 Negative Numbers
1 ZeroDefine a class with the following specifications
Class name : employee
Member variables
eno : employee number
ename : name of the employee
age : age of the employee
basic : basic salary (Declare the variables using appropriate data types)Member methods
void accept( ) : accept the details using scanner class
void calculate( ) : to calculate the net salary as per the given specificationsnet = basic + hra + da - pf
hra = 18.5% of basic
da = 17.45% of basic
pf = 8.10% of basicif the age of the employee is above 50 he/she gets an additional allowance of ₹5000
void print( ) : to print the details as per the following format
eno ename age basic net
void main( ) : to create an object of the class and invoke the methods