C Program
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char str1[100], str2[100]; int count[256] = {0}, i; printf("Enter first string: "); scanf("%s", str1); printf("Enter second string: "); scanf("%s", str2); if(strlen(str1) != strlen(str2)) { printf("Not Anagram\n"); return 0; } for(i = 0; str1[i] && str2[i]; i++) { count[(unsigned char)tolower(str1[i])]++; count[(unsigned char)tolower(str2[i])]--; } for(i = 0; i < 256; i++) { if(count[i] != 0) { printf("Not Anagram\n"); return 0; } } printf("Anagram\n"); return 0; }
C Output
Enter first string: listen Enter second string: silent Anagram
C++ Program
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string str1, str2; cout << "Enter first string: "; cin >> str1; cout << "Enter second string: "; cin >> str2; if(str1.length() != str2.length()) { cout << "Not Anagram\n"; return 0; } sort(str1.begin(), str1.end()); sort(str2.begin(), str2.end()); if(str1 == str2) cout << "Anagram\n"; else cout << "Not Anagram\n"; return 0; }
C++ Output
Enter first string: evil Enter second string: vile Anagram
JAVA Program
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first string: "); String str1 = sc.next(); System.out.print("Enter second string: "); String str2 = sc.next(); if(str1.length() != str2.length()) { System.out.println("Not Anagram"); return; } char[] arr1 = str1.toLowerCase().toCharArray(); char[] arr2 = str2.toLowerCase().toCharArray(); Arrays.sort(arr1); Arrays.sort(arr2); if(Arrays.equals(arr1, arr2)) System.out.println("Anagram"); else System.out.println("Not Anagram"); } }
JAVA Output
Enter first string: schoolmaster Enter second string: theclassroom Anagram
Python Program
def is_anagram(str1, str2): return sorted(str1.lower()) == sorted(str2.lower()) str1 = input("Enter first string: ") str2 = input("Enter second string: ") if is_anagram(str1, str2): print("Anagram") else: print("Not Anagram")
Python Output
Enter first string: debitcard Enter second string: badcredit Anagram
In-Depth Explanation
Example
Consider "listen" and "silent". They both contain the same letters: l, i, s, t, e, n. If we rearrange "silent", then we can create "listen". As each letter and frequency is the same, they are anagrams.
Now let's compare "hello" and "world". Letters don't match in number or type, so they are not anagrams.
Real-Life Analogy
Suppose you and your friend each have a bag of Scrabble tiles. Suppose your bag reads "earth" and your friend's bag reads "heart." Both bags have the same tiles, only in different order. That's what an anagram is: same pieces, different sequence.
This is closely related to verifying whether two recipes employ the same ingredients in the same amounts. If all the ingredients are the same, you can prepare the same meal even though you blend them in a different order.
Why It Matters
Anagrams might appear to be no more than an entertaining word puzzle, but the principle that lies beneath it is verifying equality of parts independent of order. This concept is significant in programming:
It introduces us to frequency counting and sorting.
It illustrates how unordered data structures can be compared.
It highlights how case insensitivity and special characters are dealt with when processing text.
This is highly applicable in data science, text analysis, natural language processing (NLP), cryptography, and search engines. For instance, Google autocorrect or spell-check algorithms frequently utilize equivalent checks to identify word variations.
Learning Insights
The problem illustrates two significant approaches:
Sorting Method: Sort the strings and compare. Easy, easy to read, and commonly used in interviews.
Hashing/Counting Method: Count occurrences of every character in both strings and compare. In some instances, quicker (O(n) time) and shows more advanced algorithmic thinking.
String manipulation, frequency analysis of characters, and considerations of complexity are learned by beginners while solving this.
Interview Use Case
This is a highly popular interview question. Interviewers ask it to try to make you think along lines more advanced than simple order comparisons. They want to know if you:
Opt for sorting because it's easy, or
Opt for hashing because it's efficient.
It also checks your knowledge of time complexity. Sorting is O(n log n), whereas counting is O(n).
Real-World Applications
An anagram check is applied in:
Plagiarism detection: Rearrangement of texts to compare.
Cryptography: Scrambled text can be compared to ensure identical patterns.
Word games and crosswords: Software such as crossword puzzle solvers or word jumble programs.
Search engines: Backing queries with re-arranged words.
SEO-Optimized Conclusion
Learning to verify an anagram in C, C++, Java, and Python is among the most popular introductory programming problems. It enables students to polish string manipulation, sorting, and hashing skills while gaining confidence in problem-solving for interviews. An anagram program is not only helpful for exams and competitive programming but also has practical uses in text matching, natural language processing, and cryptography. By learning anagram problems, students enhance their ability in string manipulation and algorithm design, which are key subjects for coding interviews and software development.
Social Plugin