#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[60];
for (;;)
{
printf("请输入任意字符串:\n");
gets(s1); //获取键盘上输入的字符串,并将其依次放入s1[0]开头的数组中。
printf("请再次输入任意字符串:\n");
gets(s2); //获取键盘上输入的字符串,并将其依次放入s2[0]开头的数组中。
printf("Length: s1 = %d, s2 = %d\n", strlen(s1), strlen(s2)); //strlen(s1):获取s1的长度;strlen(s2):获取s1的长度
if (!strcmp(s1, s2))
/*strcmp(const1, const2):
1.const1 == const2,返回0;
2.const1 > const2,返回大于0值;
3.const1 < const2,返回小于0值;
*/
printf("The string are equal.\n");
strcat(s1, s2); //将s2链接到s1之后
printf("%s\n", s1);
strcpy(s1, "I got it\n");
printf(s1); //printf("%s\n", s1);与 printf(s1);功能相同
if (strchr(s1, 'o'))
printf("Found 'o' in s1\n");
if (strstr(s1, "go"))
printf("Found 'go' in s1\n");
}
return 0;
}
/*
请输入任意字符串:
i am lovely, is that?
请再次输入任意字符串:
yes, you are all right.
Length: s1 = 21, s2 = 24
i am lovely, is that? yes, you are all right.
I got it
Found 'o' in s1
Found 'go' in s1
*/
gets,strcmp,strcat,strchr,strstr的应用
最新推荐文章于 2024-03-19 19:22:37 发布