[英]Replicating the strcmp() function from string.h library
我正在嘗試從 string.h 庫中復制 strcmp() function,這是我的代碼
/**
* string_compare - this function compares two strings pointed
* by s1 and s2. Is a replica of the strcmp from the string.h library
* @s1: The first string to be compared
* @s2: The second string to be compared
*
* Return: On success, it returns:
* 0 if s1 is equal to s2
* negative value if s1 is less that s2
* positive value if s1 is greater than s2
*/
int string_compare(char *s1, char *s2)
{
int sum = 0, i;
for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++)
sum += (s1[i] - s2[i]);
for ( ; s1[i] != '\0'; i++)
sum += (s1[i] - 0);
for ( ; s2[i] != '\0'; i++)
sum += (0 - s2[i]);
return (sum);
}
我使用以下示例代碼嘗試了我的 function:
#include <stdio.h>
int main(void)
{
char s1[] = "Hello";
char s2[] = "World!";
printf("%d\n", string_compare(s1, s2));
printf("%d\n", string_compare(s2, s1));
printf("%d\n", string_compare(s1, s1));
return (0);
}
我得到以下 output,
-53
-500
0
但我應該得到:
-15
15
0
為什么我會得到這樣的結果??
這種方法是不正確的。
假設第一個字符串是"B"
,第二個字符串是"AB"
。
很明顯,按字典順序,第一個字符串大於第二個字符串。
但是由於這個 for 循環,結果將是負面的
for ( ; s2[i] != '\0'; i++)
sum += (0 - s2[i]);
盡管 function 應返回正值。
此外,類型int
的變量sum
可能會發生溢出。
function 也應至少聲明為
int string_compare( const char *s1, const char *s2);
因為在 function 中傳遞的字符串不會改變。
function 可以通過以下方式定義
int string_compare( const char *s1, const char *s2 )
{
while ( *s1 && *s1 == *s2 )
{
++s1;
++s2;
}
return ( unsigned char )*s1 - ( unsigned char )*s2;
}
您使非常簡單的 function 過於復雜。
#define UC unsigned char
int mystrcmp(const char *s1, const char *s2)
{
int result;
while(!(result = (UC)*s1 - (UC)*s2++) && *s1++);
return result;
}
C 中的字符串是以 null 字符 ( \0
) 結尾的字符的 arrays 字符。
當您將字符串傳遞給 function 時,您傳遞的是指向其第一個元素的指針。 該指針是按值傳遞的。 您可以在 function 中修改該指針,而不會對它指向的字符串產生任何副作用,只要您不取消引用並分配給它指向的地址。
這就是0___________ 答案中的指針數學有效的原因。
int mystrcmp1(const char *s1, const char *s2) { int result = 0; while(;(result = *s1 - *s2++) && *s1++); return result; }
*s1++
可以重寫為*(s1++)
以消除歧義。 s1++
將當前指針返回到第一個字符串的開頭,然后將指針遞增,使其指向下一個字符。 然后取消引用該指針以給我們字符。 s2
指針也是如此。
然后我們通過減法比較它們。 如果它們相同,我們得到0
,在 C 中,在 boolean 上下文中為假。 此結果分配給result
。
我們現在可以看到循環繼續,而兩個字符串中的對應字符相等,而取消引用s1
並沒有給我們 null 終止符。
當循環繼續時,這意味着要么存在差異,要么我們到達了第一個字符串的末尾。
差值將存儲在 function 返回的result
中。
我不確定您使用的是什么方法,但這是給出您要求的 output 的代碼(我使用 arrays 而不是指針,因為我不擅長使用指針)。 output是基於字符串中字符的ASCII值的差異。
#include <stdio.h>
#include <string.h>
int string_compare(char s1[100], char s2[100])
{
int i,flag=0;
if(strlen(s1)==strlen(s2)){
for(i=0;i<strlen(s1);i++){
if(s1[i]!=s2[i]){
flag=1;
break;
}
}
}
if(flag){
return 0;
}
for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++){
if(s1[i]>s2[i]){
//printf("%d",s1[i]-s2[i]);
return (s1[i]-s2[i]);
}
else if (s1[i]<s2[i]){
//printf("%d",s1[i]-s2[i]);
return (s1[i]-s2[i]);
}
}
}
int main(void)
{
char s1[] = "Hello";
char s2[] = "World!";
printf("%d\n", string_compare(s1, s2));
printf("%d\n", string_compare(s2, s1));
printf("%d\n", string_compare(s1, s1));
return (0);
}
在您的代碼中,string_compare function 中的最后 2 個 for 循環是完全沒有必要的。 而且您不應該總結字符串中每個字符的 ASCII 值的差異,這是一個完全錯誤的概念。
strcmp() function 從第 0 個索引開始比較兩個字符串的字符,如果兩個字符相同,則它們的 ASCII 值也相同,因此它們的 ASCII 值的差異為 0,因此它將繼續掃描不同的字符。
如果所有字符都相同,則返回差 0。 如果在某個時候發現了不同的字符對,則返回它們在 ASCII 值中的差異,並停止掃描字符的 rest。 因此,如果字符串 1 的字符的 ASCII 值大於字符串 2 中相應字符的 ASCII 值,則差異為正。 否則為負數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.