how strcmp() compairs two strings.

Status
Not open for further replies.

sagar474

Full Member level 5
Joined
Oct 18, 2009
Messages
285
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,318
Location
India,kakinada
Activity points
3,122
I need to wright my won strcmp function. so i need to know how strcmp works.
 

Compiler source code for strcmp():

Code:
int strcmp(const char * s1, const char * s2)
{
	signed char	r;

	while(!(r = (unsigned char)*s1 - (unsigned char)*s2++) && *s1++)
		continue;

	return r;
}

Of course, each compiler has its own version, the above example is typical of an ANSI C compiler.

Check your GCC compiler for its source code for strcmp().

Hope the info helps,

BigDog
 
int strcmp1(char *str1, char *str2)
{
while ((*str1 == *str2) && (*str1 != '\0'))
{
str1++;
str2++;
}

if (*str1 > *str2)
return 1;

if (*str1 < *str2)
return -1;

return 0;
}
 
A function! it is a function that compare two strings(that i think)..see this one

int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}
 

Hi

void strcmp(char *p,char*s)
{
while(*p)
{
if(*p++==*s++)
printf("strings are equal");
else
printf("strings are not equal");
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…