(c语言数组的!)比较两个字符串的大小。(不可以用strcmp做)

2025-02-23 18:52:10
推荐回答(4个)
回答1:

main(){
int i=0,j=0;
char str1[]="china";
char str2[]="word";
char min,max;
while(str1[i]!='\0')i++;
i--;
while(str2[j]!='\0')j++;
j--;
if(i>j) max=str2 min=str1; 这里和下面的if这里都没有{};
所以无论if对或不对 ,对min的负值
始终实行
if(j>i) max=str1 min=str2;
printf("%c %c",min,max);
}首先这个程序并不是求字符串的大小的,而是求哪个字符串的长度长。。

回答2:

/***
*strcmp.c - routine to compare two strings (for equal, less, or greater)
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Compares two string, determining their lexical order.
*
*******************************************************************************/

/***
*strcmp - compare two strings, returning less than, equal to, or greater than
*
*Purpose:
* STRCMP compares two strings and returns an integer
* to indicate whether the first is less than the second, the two are
* equal, or whether the first is greater than the second.
*
* Comparison is done byte by byte on an UNSIGNED basis, which is to
* say that Null (0) is less than any other character (1-255).
*
*Entry:
* const char * src - string for left-hand side of comparison
* const char * dst - string for right-hand side of comparison
*
*Exit:
* returns -1 if src < dst
* returns 0 if src == dst
* returns +1 if src > dst
*
*Exceptions:
*
*******************************************************************************/

int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;

while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;

if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;

return( ret );
}

/*
* linux/lib/string.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/

/*
* stupid library routines.. The optimized versions should generally be found
* as inline code in
*
* These are buggy as well..
*
* * Fri Jun 25 1999, Ingo Oeser
* - Added strsep() which will replace strtok() soon (because strsep() is
* reentrant and should be faster). Use only strsep() in new code, please.
*/

/**
* strcmp - Compare two strings
* @cs: One string
* @ct: Another string
*/
int strcmp(const char * cs,const char * ct)
{
register signed char __res;

while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
}

return __res;
}

回答3:

你这是在比较那个字符串更长嘛 那是比较大小呢。
max和min也应该申明为int类型,因为str1和str2是char指针,你这样直接赋值也是不对的。

回答4:

可一按照strcmp的原理,
对字符串数组的的每一个元素进行对比