很多C代码
源代码在线查看: 7-16.txt
/* 范例:7-16 */
#include
#include
void main()
{
printf("=====strcmp======\n");
char *s1 = "abc";
char *s2 = "arc";
printf("strcmp: %d\n",strcmp(s1,s2));
if(strcmp(s1,s2) printf("strcmp(s1,s2) char *s3 = "aar";
char *s4 = "aaadef";
printf("strcmp: %d\n",strcmp(s3,s4));
if(strcmp(s3,s4)>0)
printf("strcmp(s3,s4)>0\n");
char *s5 = "abcd";
char *s6 = "abcd";
printf("strcmp: %d\n",strcmp(s5,s6));
if(strcmp(s5,s6)==0)
printf("strcmp(s5,s6)=0\n");
printf("=====strlen======\n");
printf("strlen(s6): %d\n",strlen(s6));
printf("=====strchr======\n");
char *string = "tiger is brave";
char *ptr, c = 'e';
ptr = strchr(string,c);
printf("strchr(string,c): %c\n",*ptr);
if (*ptr==c)
printf("字符'%c'在字符串tiger is brave的第%d个位置\n", c, \ (ptr-string)+1);
printf("=====strstr======\n");
char *str1 = "tiger is brave", *str2 = "ra", *strptr;
strptr = strstr(str1, str2);
printf("%s\n", strptr); /* 输出字符指针strptr所指向地址到'\0'的字符串 */
getchar();
}
程序执行结果:
=====strcmp======
strcmp: -16
strcmp(s1,s2) strcmp: 17
strcmp(s3,s4)>0
strcmp: 0
strcmp(s5,s6)=0
=====strlen======
strlen(s6): 4
=====strchr======
strchr(string,c): e
字符'e'在字符串tiger is brave的第4个位置
=====strstr======
rave