在 C 语言中处理有关 strncmp 的一些代码时遇到一些困难
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有几个问题:
strncmp()
,strcmp()
即可工作。1
或0
,而不是1
或-1
。You have several problems:
strncmp()
,strcmp()
will work.1
or0
, not1
or-1
.对于初学者来说,函数定义中函数参数的类型与
先前函数声明中的类型不对应。
至少你需要在函数定义中编写
但是这样声明函数会更正确,
因为传递的字符串在函数内不会改变。
您使用函数
strnmp
的方法没有多大意义。而且这个调用
是不正确的。例如,为什么使用表达式
shortStr + offset
?如果函数的目的是确定第一个字符串是否以第二个字符串结尾,则可以按以下方式定义该函数
或者如果您想在成功时返回 1,否则返回 0,则函数定义可以如下所示
For starters the type of the function parameters in the function definition
does not correspond to the type in the previous function declaration.
At least you need to write in the function definition
But it would be more correct to declare the function like
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
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
Or if you want to return 1 in the case of success or 0 otherwise then the function definition can look like
您的代码存在许多问题。
您的
endsWith()
声明与其定义不匹配。main()
忽略endsWith()
的返回值
根本不需要
endsWith()
中的循环。您的方法是实现indexOf()
函数而不是endsWith()
函数。您可以通过一次减法计算所需的偏移量
,无需循环。但是,即使需要循环,您也无法正确计算循环条件。您正在从较短的值中减去较大的值,这将导致负值。但由于
strlen()
返回一个无符号类型,负数将转换为非常大的正值,因此最终循环次数超出了您的需要。您向
strncmp() 传递了错误的参数
。由于您想要比较longStr
的末尾以查看它是否与shortStr
匹配,因此需要传递longStr + offset
而不是>shortStr + offset
,并传递strlen(shortStr)
而不是strlen(longStr)
。endsWith()
应返回0
,而不是-1
。话虽如此,尝试更像这样的事情:
There are a number of problems with your code.
your declaration of
endsWith()
does not match its definition.main()
is ignoring the return value ofendsWith()
there is no need for a loop in
endsWith()
at all. Your approach is implementing anindexOf()
function rather than anendsWith()
function. You can calculate the neededoffset
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 thelongStr
to see if it matches theshortStr
, you need to passlongStr + offset
instead ofshortStr + offset
, and also passstrlen(shortStr)
instead ofstrlen(longStr)
.endsWith()
should return0
if a match is not found, not-1
.With that said, try something more like this instead: