strncmp是strcmp的进阶版
链接介绍一下strcmp(c基础)-CSDN博客
作用
比较两个字符串的前n位
格式
#include <string.h>
strncmp (arr1,arr2,n);
工作原理:strcmp函数按照ACII(字符编码顺序)比较两个字符串。它从两个字符串的第一个字符开始逐个比较,直到遇到不同的字符或者到达字符串的末尾(即遇到\0)或比较过n个字符。
如果arr1< arr2(即某次比较arr1的ACII小于arr2的)返回一个<0的值(一般为-1)
如果arr1= arr2、)返回0
如果arr1 > arr2(即某次比较arr1的ACII大于arr2的)返回一个>0的值(一般为1)
#include<stdio.h>
#include<string.h>//strcmp比较字符串
//int main()
//{
//
// char arr1[20] = "I am 36";
// char arr2[20] = "I am DRAGON";
//
// int result = 0;
// result = strcmp(arr1, arr2);
//
// if (result < 0)
// printf("arr1 is less than arr2");
// else if (result == 0)
// printf("arr1 is equl to arr2");
// else
// printf("arr1 is more than arr2");
//
//
// return 0;
//}//strncmp比较字符串
int main()
{char arr1[20] = "I am 36";char arr2[20] = "I am DRAGON";int result = 0;result = strncmp(arr1, arr2,5);if (result < 0)printf("arr1 is less than arr2");else if (result == 0)printf("arr1 is equl to arr2");else printf("arr1 is more than arr2");return 0;
}
hi I am 36.thanks for your looking .👍👍👍