All about programming in GNU/LINUX

string compare Code , with a core Function called Lsearch which is capable of handling any data type , The CMP function is type specific.

#include<stdio.h>

#include<string.h>

#include<stdlib.h>
void *cmp(void *str1,void *str2);
void *lsearch(void *key,void *base,int n,int size);
int main()
{
char *str[]={“karttttttttttttt”,”elephantsec”,”elephant”,”Eb”,”D#”,”C#”};
char *key=”elephant”;//hops
printf(“\n%d\n”,sizeof(char **));
lsearch(&key,str,5,sizeof(char *));

return 0;

}
void *lsearch(void *key,void *base,int n,int size)
{
int i;
for(i=0;i<n;i++)
{
void *elmntadd=(char *)base+(i*size);
printf(“\n%u\n”,elmntadd);
if(cmp(key,elmntadd)==0)//why are sending values
{
printf(“Match found at %dth position”,i+1);
return 0;
}

}
printf(“\nMatch not found”);
return NULL;
}
void *cmp(void *str1,void *str2)
{
char *s1=*(char **)str1;
char *s2=*(char **)str2;

return(strcmp(s1,s2));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s