Monday, 24 March 2014

write a program to check two string are anagram or not.

/////////////////////////////////////////////////////////////definition//////////////////////////////////////////////////////////////////////////////////
what is anagram ?
ans. when  letter in two word is of same type even position is different and letters are uppercase or lowercase then these words are called as anagram.eg 
ist word :CproGRAM
2nd word:cmargPOR
these words are anagram .

/////////////////////////////////////////////////////////////////hint/////////////////////////////////////////////////////////////////////////////////////

/* write a program for anagram
   hint: first remove the integer from  both string
         second convert the both string either uppercase or lowercase
         third sort the both string alphabetically 
         fourth and last compare both string by strcmp if both string equal then anagram otherwise not anagram */
/////////////////////////////////////////////////////////program//////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
int i,j,n1,n2,n3,n4;
char temp[90]={0},temp1[90]={0};
char s1[90],s2[90],ch=0,ch1=0,*p1,*p2;
main()
{
puts("enter first string:");
scanf(" %[^\n]s",s1);
puts("enter second string:");
scanf(" %[^\n]s",s2);
n1=strlen(s1);
n2=strlen(s2);
j=0;
for(i=0;i<n1;i++)
 {
  if(!(s1[i]>=48 && s1[i]<=57))
{     
temp[j]=s1[i];
 j++;
   }
}
 temp[j]='\0';
j=0;
for(i=0;i<n2;i++)
 {
  if(!(s2[i]>=48 && s2[i]<=57))
{     
temp1[j]=s2[i];
 j++;
   }
}
 temp1[j]='\0';
n3=strlen(temp);
 for(i=0;i<n3;i++)
{
 if(temp[i]>=97 && temp[i]<=122)
 {
  temp[i]=temp[i]-32;
 }
}
n4=strlen(temp1);
for(i=0;i<n4;i++)
{
 if(temp1[i]>=97 && temp1[i]<=122)
  {
   temp1[i]=temp1[i]-32;
}
}
p1=temp;
for(i=n3-1;i>0;i--)
 {
  for(j=0;j<i;j++)
   {
     if(p1[j]>p1[j+1])
     {
     ch=p1[j];
   p1[j]=p1[j+1];
   p1[j+1]=ch;
    }
  }
 }
for(i=n4-1;i>0;i--)
 {
  for(j=0;j<i;j++)
   {
     if(temp1[j]>temp1[j+1])
     {
     ch1=temp1[j];
   temp1[j]=temp1[j+1];
   temp1[j+1]=ch1;
    }
  }
 }
if((strcmp(p1,temp1)==0))
  {
   printf("ANAGRAM\n");
  }
  else
{
  printf("NOT ANAGRAM\n");
 }

}//main

No comments:

Contact Form

Name

Email *

Message *