To find the anagram in a string array

Question:
It’s about anagram. Input was the array of strings and a word was given to find whether it has anagram in the given array.
Logic:
- Get the string array to check and also a word to check.
- Find the length of string array b[].
- Then generate all combinations of the string using the find combination logic.
- Pass the combination, a, l value, and size of b[] to the function.
- Compare a with b[] using strcmp() function.
- If found means print “found” with its location.
- Otherwise, check all other combinations.
Program:
[code lang=”c”]
#include<stdio.h>
int find(char a[],char *b[],int m,int l)
{
int i;
for(i=0;i<m;i++)
{
if(strcmp(a,b[i])==0) //checks for string in b[]
{ l=1;
printf("%s is anagram found at %d\n",b[i],i+1); //prints when found with location
}
}
return l;
}
void main()
{
char a[20],*b[]={"tea","play","you","eat"};
int i,j,n,k,temp,m=0,l=0;
clrscr();
printf("enter the string:");
gets(a);
n=strlen(a);
for(i=0;b[i]!=’\0’;i++) //find the length of b[]
{
m++;
}
for(j=0;j<n;j++) //generate all combinations of string
{
for(k=n-1;k>0;k–)
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
l=find(a,b,m,l); //pass to function for comparison
}
}
if(l==0) //no anagram found
{
printf("No annagram found");
}
getch();
}
[/code]
You might also like:
To find the substring in a string