头文件:#include<string.h>
函数声明:int strcmp(const char *s1,const char*s2);
函数说明:比较s1和s2指向的字符串的大小。
比较的方法:逐个字符去比较ascII码,一旦比较出大小返回。如果所有字符都一样,则返回0。
返回值:如果s1指向的字符串大于s2指向的字符串返回1,如果s1指向的字符串小于s2指向的字符串返回-1,如果相等的话返回0
函数声明: int strncmp(const char* s1,const char* s2, size_t n);
函数说明:比较s1和s2指向的字符串中的前n个字符
- #pragma warning(disable:4996)
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include <string.h>
- int main()
- {
- char* str1 = "helloworld";
- char* str2 = "hellokitty";
- if (strcmp(str1, str2) == 0)
- {
- printf("str1==str2\n");
- }
- else if(strcmp(str1, str2) > 0)
- {
- printf("str1>str2\n");
- }
- else
- {
- printf("str1<str2\n");
- }
- if (strncmp(str1, str2, 5) == 0)
- {
- printf("str1==str2\n");
- }
- else if(strncmp(str1, str2, 5) > 0)
- {
- printf("str1>str2\n");
- }
- else
- {
- printf("str1<str2\n");
- }
- return 0;
- }
————————————————
版权声明:本文为CSDN博主「害恶细君」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_50216991/article/details/141871740
|