Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Wednesday 19 June 2019

Anagram or not.

Question:

















Approach:


Step 1: Check Both the String For Equal Length.
if equal then continue, else print Not Anagram.

Step 2: Take A Array for All the Alphabets
And Calculate the no of times that  present in the both String

Step 3: Comparing the Array if equal then Anagram 
Else not Anagram.



Solution:

#include<stdio.h>
int main()
{
char a[10],b[10];
int i,j,k,m,n,z[26]={0},y[26]={0},flag,c,d;
printf("enter the 2 string");
scanf("%s %s",a,b);
if(strlen(a)!=strlen(b))
 printf("not an anagram");
else
{
    k=0;
while(k!=26)
{
n=1;
m=1;
for(i=0;i<strlen(a);i++)
{
c=(int)a[i]-97;
d=(int)b[i]-97;
if(k==d)
{
  y[k]=m++;

}

 if(k==c)
  {
           z[k]=n++;
}
}
k++;
}

for(j=0;j<26;j++)
{
if(z[j]!=y[j]){
printf("Not anagram");
return 0;
}
}
    printf("Anagram");
}
return 0;
}


//Credits Rakshita.

0 comments:

Post a Comment