- /* k&r(5.5): character pointers and functions
-
created on Mar 21, 2011
-
*/
-
#include "stdio.h"
-
-
int pstrcmp(char *, char *);
/* main: compares two character strings s and t, and return negative, zero or position if s
t!
The value is obtained by substracting the characters at the first position where s and t disagree.
*/
-
int main()
-
{
-
int i;
-
char str1[] = "hello";
-
char str2[] = "hello mark";
-
-
i = pstrcmp(str1, str2);
-
printf("%d\n", i);
-
}
-
-
/* strcmp: return <0 if s0 if s>t */
-
int astrcmp(char *s, char *t)
-
{
-
int i;
-
-
for (i = 0; s[i] == t[i]; i++)
-
if (s[i] == '\0')
-
return 0;
-
return s[i] - t[i];
-
}
-
-
/* strcmp: pointer version */
-
int pstrcmp(char *s, char *t)
-
{
-
for(; *s == *t; s++, t++)
-
if (*s == '\0')//also *t=='\0'
-
return 0;
-
return *s - *t;
-
}