Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Anagram in C++

Anagram:
The algorithm here we constructed to find the number of characters in the given 2 strings and to compare each and every character in both the strings to be same is called as anagram
Example:
if string1="forget" and string2="oretfg"
In the program the string length are same and each character in both the strings are present one time.so this strings are called anagram


// TO CHECK WHETHER TWO STRINGS ARE ANAGRAMS OR NOT
#include<iostream.h>

#include<conio.h>
#include<stdio.h>
class Anagram
{
public:

int check_anagram( char a[],char b[])
{
int first[26]={0},second[26]={0},c=0;
while(a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c=0;
while( b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for(c=0;c<26;c++)
{
if(first[c] != second[c])
return 0;
}
return 1;
}
};
void main()
{
Anagram x;
char str1[100],str2[100];
int flag;
clrscr();
cout<<"\nEnter the first string :";
gets(str1);
cout<<"\nenter the second string :";
gets(str2);
flag= x.check_anagram(str1,str2);

if(flag == 1)
cout<<str1<<"\tand\t"<<str2<<" are anagrams.";
else
cout<<str1<<"\tand\t"<<str2<<" are not anagrams.";
getch();
}