Problem Statement: Write a function to check whether two given strings are anagram of each other or not. Two strings are Anagram of each other if both the string contains the same characters, only the order of characters can be different. For example, “mycomputerknowledge” and “myknowledgecomputer” are an anagram of each other. |
#include<stdio.h> int isAnagram(char str1[],char str2[]) { int count1[26]={0},count2[26]={0},c=0; while (str1[c] != '\0') { count1[str1[c]-'a']++; c++; } c = 0; while (str2[c] != '\0') { count2[str2[c]-'a']++; c++; } for (c = 0; c < 26; c++) { if (count1[c] != count2[c]) return 0; } return 1; } int main(){ char str1[]="mycomputerknowledge"; char str2[]="myknowledgecomputer"; if(isAnagram(str1,str2)) { printf("\nString is Anagram!"); } else { printf("\nString is not Anagram"); } return 0; }
Output
$gcc anagram.c $ ./a.out String is Anagram!$ |
also see
C Programming language |
Go Programming language |
Linked List | Array |
Stack | Queue |
Puzzle | Reasoning |
Aptitude | HTML |