Have you ever come across the term Anagram and are wondering what it is? If yes, then don't worry because, in this tutorial, we will see what an anagram is and an example to check whether two strings are anagrams in Java and C or not in two different programming languages.
So let's get started.
What are Anagrams in Java?
An Anagram is a word or a phrase that is formed by rearranging the letters and words of a different word or phrase. For example, the word dormitory itself can be arranged into a dirty room also the word conversation can be converted into voices rant on. So, in simple terms, we can say that a word or phrase made by transposing the letters of another word or phrase is known as an anagram.
Here, the original word is termed as the subject of an anagram, and the word that is produced after rearranging the letters of the original word is termed as an anagram.
Let us look at the examples to check whether two strings are anagrams or not.
Check Whether Anagram or Not In C
In this example, we will see how to check whether two strings are anagrams or not in C.
Algorithm:
- Start
- Declare two character arrays.
- Initialize both array.
- Declare variables to store the length.
- Check whether the length of the two strings is equal or not.
- If equal, then use two for loops to check whether the strings are anagrams or not.
- Else print that both the strings are not an anagram.
- Display the result.
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[20]="care";
char str2[20]="race";
int len, len1, len2, i, j, flag=0, nflag=0;
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 == len2)
{
len = len1;
for(i=0; i<len; i++)
{
flag = 0;
for(j=0; j<len; j++)
{
if(str1[i] == str2[j])
{
flag = 1;
break;
}
}
if(flag == 0)
{
nflag = 1;
break;
}
}
if(nflag == 1)
printf("\nStrings are not Anagram");
else
printf("\nStrings are Anagram");
}
else
printf("\nBoth string must contain same number of character to be an Anagram Strings");
return 0;
}
Strings are Anagram
Check Whether Anagrams in Java or Not
In this example, we will see how to check whether two strings are anagrams in Java or not.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare two strings.
- Ask the user to initialize these variables.
- Convert the two strings into lowercase.
- Now, convert the strings to a character array.
- Call a boolean-type user-defined function to check the anagram.
- Take both the character array as parameters.
- First, find the length of both the character arrays.
- Now, sort both arrays.
- Using a for loop check whether the two anagrams are equal or not.
- Return the boolean result.
- Display the output
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
//Java program to check whether two strings are anagrams of each other
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.*;
public class Main
{
static boolean areAnagram(char[] str1, char[] str2)
{
int n1 = str1.length;
int n2 = str2.length;
if (n1 != n2)
return false;
Arrays.sort(str1);
Arrays.sort(str2);
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
System.out.println("The entered string is: "+str1);
System.out.println("The entered string is: "+str2);
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();;
if (areAnagram(charArray1, charArray2))
System.out.println("The two strings are anagram of each other");
else
System.out.println("The two strings are not anagram of each other");
}
}
The entered string is: the study
The entered string is: dusty
The two strings are an anagram of each other.
Conclusion:
Anagrams in Java are a fascinating and important topic in computer programming.
Being able to recognize and manipulate anagrams can be useful in many contexts, such as in cryptography, data compression, and natural language processing.
By implementing an anagram program in Java, you can not only sharpen your programming skills but also explore the intriguing world of wordplay and language manipulation. So why not give it a try and see what anagrams in Java can reveal to you? Happy coding!
Frequnetly Asked Questions(FAQs)
1. What is anagram C program?
An anagram C program is a program written in the C programming language that determines if two input strings are anagrams or not.
2. How to write anagram in C?
To write an anagram program in C, you can sort the characters of each string and compare them. If the sorted strings are equal, then the input strings are anagrams.
3. How to make anagram in Java?
To make an anagram program in Java, you can use a similar approach as in C, i.e. sort the characters of each string and compare them. Alternatively, you can count the occurrence of each character in both strings and compare the counts.
4. What is an anagram number example?
An anagram number is a number whose digits can be rearranged to form a different number. For example, 123 and 231 are anagrams of each other, so both are anagram numbers.
You may also like: