字符串比较

字符串比较函数实现
本文介绍了一个特殊的字符串比较函数strnumcmp的实现方法,该函数能够正确处理包含数字的字符串比较,如abc100>abc2的情况,并详细展示了其工作原理及调用示例。
#include <stdio.h>

int strnumcmp(char str1[], char str2[]){
	int n=0;
	int t1=0;
	int t2=0;

	while( *str1 && !(n=(*str1)-(*str2)) ){
		str1++;
		str2++;
	}

	while( (*str1)<='9' && (*str1)>='0' ){
		t1=t1*10+(*str1-0x30);
		str1++;
	}

	while( (*str2)<='9' && (*str2)>='0' ){
		t2=t2*10+(*str2-0x30);
		str2++;

		n=t1-t2;
	}

	return n;

}

void main(){
	char a[]="abc2";
	char b[]="abc100";

	if( strnumcmp(a,b)>0 ){
		printf("%s>%s\n",a,b);
	}else if( strnumcmp(a,b)==0 ){
		printf("%s=%s\n",a,b);
	}else{
		printf("%s<%s\n",a,b);
	}

}

 

这是一道笔试题目:编写 strnumcmp 函数,"abc100">"abc2",其他功能同strcmp函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值