在 C 中圍繞 strncmp 的一些代碼有一些困難只是想知道是否有人遇到了同樣的問題讓我
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(char* longStr, char* shortStr);
int main()
{
char* longString = "Grant is The man";
char* shortString = "man";
endsWith(longString,shortString);
printf("%s\n",shortString);
}
int endsWith(char longStr, char shortStr)
{
int offset;
for (offset = 0 ; offset < strlen(shortStr) - strlen(longStr) ; offset )
if (strncmp( longStr , shortStr offset , strlen(longStr)) == 0)
return 1;
return -1;
}
回傳應該是人,如果我插入的是什么都應該回傳或 0。
uj5u.com熱心網友回復:
你有幾個問題:
- 不需要回圈。只需獲取偏移量并從那里比較一次。
- 您正在向后計算偏移量。您需要從長長度中減去短長度。
- 沒有必要使用
strncmp(),strcmp()會作業。 - 一個測驗函式應該回傳
1or0, not1or-1。 - 你永遠不會檢查函式的結果。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(char* longStr, char* shortStr);
int main()
{
char* longString = "Grant is The man";
char* shortString = "man";
if (endsWith(longString,shortString)) {
printf("%s\n",shortString);
}
}
int endsWith(char longStr, char shortStr)
{
int offset = strlen(longStr) - strlen(shortStr);
if (offset < 0) { // shortStr is longer than longStr
return 0;
}
return strcmp(shortStr, longStr offset) == 0;
}
uj5u.com熱心網友回復:
對于初學者,函式定義中函式引數的型別
int endsWith(char longStr, char shortStr)
與前面函式宣告中的型別不對應。
至少你需要在函式定義中寫
int endsWith(char *longStr, char *shortStr)
但是像這樣宣告函式會更正確
int endsWith( const char *longStr, const char *shortStr );
因為傳遞的字串在函式內沒有改變。
您使用該功能的方法strncmp沒有多大意義。
而且這個電話
strncmp( longStr , shortStr offset , strlen(longStr))
只是不正??確。例如,為什么使用運算式shortStr offset?
如果該函式的目的是確定第一個字串是否以第二個字串結尾,則可以通過以下方式定義該函式
int endsWith( const char *longStr, const char *shortStr );
{
size_t n1 = strlen( longStr );
size_t n2 = strlen( shortStr );
int success = -1;
if ( !( n1 < n2 ) )
{
if ( strcmp( longStr n1 - n2, shortStr ) == 0 ) success = 1;
}
return success;
}
或者,如果您想在成功的情況下回傳 1,否則回傳 0,那么函式定義可能看起來像
int endsWith( const char *longStr, const char *shortStr );
{
size_t n1 = strlen( longStr );
size_t n2 = strlen( shortStr );
return !( n1 < n2 ) && strcmp( longStr n1 - n2, shortStr ) == 0;
}
uj5u.com熱心網友回復:
您的代碼存在許多問題。
您的宣告
endsWith()與其定義不符。main()忽略回傳值endsWith()根本不需要回圈
endsWith()。您的方法是實作一個indexOf()功能而不是一個endsWith()功能。您可以通過offset單次減法計算所需,無需回圈。但是,即使需要回圈,您也沒有正確計算回圈條件。您正在從較短的值中減去較大的值,這將導致負值。但是由于
strlen()回傳一個無符號型別,一個負數將包裝成一個非常大的正值,所以你最終回圈的次數超出了你的需要。您將錯誤的引數傳遞給
strncmp(). 既然要比較 結尾longStr看是否匹配shortStr,就需要通過longStr offset代替shortStr offset,也需要通過strlen(shortStr)代替strlen(longStr)。endsWith()0如果未找到匹配項,則應回傳,而不是-1.
話雖如此,請嘗試更像這樣的東西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(const char* longStr, const char* shortStr);
int main()
{
const char* longString = "Grant is The man";
const char* shortString = "man";
if (endsWith(longString, shortString))
printf("Ends with %s\n", shortString);
else
printf("Does not end with %s\n", shortString);
}
int endsWith(const char* longStr, const char* shortStr)
{
size_t longLen = strlen(longStr);
size_t shortLen = strlen(shortStr);
return (
(longLen >= shortLen) &&
(strncmp(longStr (longLen-shortLen), shortStr, shortLen) == 0)
) ? 1 : 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448234.html
