string函数
/* strcpy: copy t to s */
char *strcpy(char *s, char *t)
{
char *p = s;
while(*s++ = *t++)
;
return p;
}
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t)
{
for(; *s == *t; s++, t++)
if(*s == '\0')
return 0;
return *s - *t;
}
/* strcat: concatenate t to end of s; s must be big enough */
char *strcat(char *s, char *t)
{
char *p = s;
while(*s++)
;
s--;
while(*s++ = *t++)
;
return p;
}
/* strend: return 1 if t appears at the end of s, else return 0 */
int strend(char *s, char *t)
{
int i = 0;
while(*t++)
i++;
while(*s++)
;
while(i-- >= 0 && --t == --s)
if(i == 0)
return 1;
return 0;
}
#include <stdio.h>
int main()
{
char s[100] = "saturday";
char t[100] = "sunday";
printf("strcmp: %d\n", strcmp(s,t));
printf("strcat: %s\n", strcat(s,t));
printf("strend: %d\n", strend(s,t));
printf("strcpy: %s\n", strcat(s,t));
return 0;
}
- 运行结果
strcmp: -20
strcat: saturdaysunday
strend: 1
strcpy: saturdaysundaysunday