在 C 语言中处理有关 strncmp 的一些代码时遇到一些困难

发布于 2025-01-16 01:31:56 字数 682 浏览 0 评论 0原文

在 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;
}

返回应该是 man,如果我插入的是不应该返回任何内容或 0。

having some difficulty with some code around strncmp in C just wondering if there is someone that ran into the same problem has me

#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;
}

the return should be man and if i inserted is the nothing should be returned or 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魄砕の薆 2025-01-23 01:31:56

您有几个问题:

  • 不需要循环。只需获取偏移量并从那里比较一次即可。
  • 您正在向后计算偏移量。您需要从长长度中减去短长度。
  • 无需使用 strncmp()strcmp() 即可工作。
  • 测试函数应返回 10,而不是 1-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;
}

You have several problems:

  • There's no need for a loop. Just get the offset and compare from there once.
  • You're calculating the offset backwards. You need to subtract the short length from the long length.
  • There's no need to use strncmp(), strcmp() will work.
  • A testing function should return 1 or 0, not 1 or -1.
  • You're never checking the result of the function.
#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;
}
静待花开 2025-01-23 01:31:56

对于初学者来说,函数定义中函数参数的类型与

int endsWith(char longStr, char shortStr)

先前函数声明中的类型不对应。

至少你需要在函数定义中编写

int endsWith(char *longStr, char *shortStr) 

但是这样声明函数会更正确,

int endsWith( const char *longStr, const char *shortStr );

因为传递的字符串在函数内不会改变。

您使用函数 strnmp 的方法没有多大意义。

而且这个调用

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;
}

For starters the type of the function parameters in the function definition

int endsWith(char longStr, char shortStr)

does not correspond to the type in the previous function declaration.

At least you need to write in the function definition

int endsWith(char *longStr, char *shortStr) 

But it would be more correct to declare the function like

int endsWith( const char *longStr, const char *shortStr );

because the passed strings are not changed within the function.

Your approach with the function strncmp does not make a great sense.

Moreover this call

strncmp( longStr , shortStr + offset , strlen(longStr))

is just incorrect. For example why is there used the expression shortStr + offset?

If the purpose of the function is to determine whether the first string is ended with the second string then the function can be defined the following way

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;
}

Or if you want to return 1 in the case of success or 0 otherwise then the function definition can look like

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;
}
一桥轻雨一伞开 2025-01-23 01:31:56

您的代码存在许多问题。

  • 您的 endsWith() 声明与其定义不匹配。

  • main() 忽略 endsWith()

    的返回值

  • 根本不需要 endsWith() 中的循环。您的方法是实现 indexOf() 函数而不是 endsWith() 函数。您可以通过一次减法计算所需的偏移量,无需循环。

    但是,即使需要循环,您也无法正确计算循环条件。您正在从较短的值中减去较大的值,这将导致负值。但由于 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;
}

There are a number of problems with your code.

  • your declaration of endsWith() does not match its definition.

  • main() is ignoring the return value of endsWith()

  • there is no need for a loop in endsWith() at all. Your approach is implementing an indexOf() function rather than an endsWith() function. You can calculate the needed offset with a single subtraction, no looping required.

    But, even if a loop were needed, you are not calculating the loop condition correctly. You are subtracting a larger value from a shorter value, which will result in a negative value. But since strlen() returns an unsigned type, a negative will wrap to a very large positive value, so you end up looping more than you need.

  • you are passing the wrong parameters to strncmp(). Since you want to compare the end of the longStr to see if it matches the shortStr, you need to pass longStr + offset instead of shortStr + offset, and also pass strlen(shortStr) instead of strlen(longStr).

  • endsWith() should return 0 if a match is not found, not -1.

With that said, try something more like this instead:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文