c语言作业 比较字符串,C语言strncmp()函数:对指定字符串数量的两个字符串进行比较...

函数名: strncmp

头文件:

函数原型: int strncmp(const char *str1,const char *str2,int n);

功 能:  对指定字符串数量的两个字符串进行比较

参 数:  str1和str2   为要进行比较的字符串

int n         为要比较的字符串个数

返回值: str1 > str2   返回大于0的值;

str1==str2    返回等于0的值;

str1 < str2   返回小于0的值;

注 意:此函数返回的不是1或-1这样的固定值,而是大于0或小于0的值

程序例:将字符串s2分别于字符串s1和s3的前n个字符进行比较,并把结果输出#include

#include

int main(void){

char *s1="www.dotcpp",*s2="dotcpp.com",*s3="dotcpp";

int p=strncmp(s2,s1,3);

if(p>0){

printf("s2 is greater than s1\n");

}else if(p<0){

printf("s2 is less than s1\n");

}else{

printf("s2 is equals s1\n");

}

p=strncmp(s2,s3,3);

if(p>0){

printf("s2 is greater than s3\n");

}else if(p<0){

printf("s2 is less than s3\n");

}else{

printf("s2 is equals s3\n");

}

return 0;

}

运行结果:s2 is less than s1

s2 is equals s3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值