EmbLogic's Blog

c program to compare two string using function,pointer and memory allocation technique

#include
#include
#include
char *input();
int cmp(char * ,char *);
int main()
{
char *a,*b;
int i;
printf(“Enter 1st string:”);
a=input();
printf(“Enter 2nd string:”);
b=input();
i=cmp(a,b);
printf(“Comparision is=%d\n”,i);
}
char *input()
{
char *s1,ch;
int i=0;
s1=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
s1=realloc(s1,(i+2)*sizeof(char));
if(ch==10)
break;
s1[i]=ch;
i++;
}
return s1;
}
int cmp(char *a,char *b)
{
int i,j,flag=0;
for(i=0;i<=5;i++)
{
if(*(a+i)!=*(b+i))
{
j=*(a+i)-*(b+i);
return j;

}
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>