以。。结束() ? [英] ends_with() ?

查看:65
本文介绍了以。。结束() ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


有没有人知道标准功能或更简单的方法

来获得这个结果。


int ends_with(char * string,char * has);


回答字符串是否以子字符串结尾。

我的第一种方法:


int ends_with(char * name,char * has){

int name_len = strlen(name);

int has_len = strlen (有);

返回

name_len> = has_len&&

!memcmp(name + name_len-has_len,has,has_len) ;

}


祝福


Andreas Klimas

Hello,

does anybody know about a standardfunction or a simpler way
to get this result.

int ends_with(char *string, char *has);

answer whether string ends with substring has or not.
my first approach:

int ends_with(char *name, char *has) {
int name_len = strlen(name);
int has_len = strlen(has);
return
name_len>=has_len &&
!memcmp(name+name_len-has_len, has, has_len);
}

best wishes

Andreas Klimas

推荐答案

Andreas Klimas写道:
Andreas Klimas wrote:

你好,

有人知道标准功能还是更简单的方法
得到这个结果。

int ends_with(char * string,char * has);

回答字符串是否以子字符串结尾。

我的第一种方法:

in t ends_with(char * name,char * has){
int name_len = strlen(name);
int has_len = strlen(has);
返回
name_len> = has_len& ;&
!memcmp(name + name_len-has_len,has,has_len);
}

Hello,

does anybody know about a standardfunction or a simpler way
to get this result.

int ends_with(char *string, char *has);

answer whether string ends with substring has or not.

my first approach:

int ends_with(char *name, char *has) {
int name_len = strlen(name);
int has_len = strlen(has);
return
name_len>=has_len &&
!memcmp(name+name_len-has_len, has, has_len);
}




这看起来并不可怕,虽然我不会这样做很好

就好。


理想情况下,如果可以的话,你应该传递长度。重新计算

这些东西需要时间,所以如果你已经掌握了这些信息,你需要提供它。


但是如果你必须的话如果不事先知道你的b / b $ b $字符串有多长,我就会这么做,就像你做的那样:


#include< assert .h>

#include< string.h>


int ends_with(const char * name,const char * has)

{

int rc = 0; / * 0表示否,1表示是,表示是。 * /

size_t name_len;

size_t has_len;


断言(姓名!= NULL);

断言(有!= NULL);


name_len = strlen(name);

has_len = strlen(has);


if(name_len> = has_len)

{

name + = name_len;

name - = has_len;

if(strcmp(name,has)== 0)

{

rc = 1;

} < br $>
}


返回rc;

}


使用标准库函数的替代方案是可能是笨拙的b $ b笨拙。例如,考虑一下strstr。你必须循环它

以防子串在字符串中较早出现但最后不是
。 strrchr听起来像是一个不错的选择,直到你认为它是通过它来实现的。


你所拥有的指针算术理念已经足够了。注意我的

使用const,size_t和assert。此外,我已经简化了

代码,这样每行只做一个工作而不是六个工作。 :-)



That doesn''t look terrible, although I wouldn''t do it quite
like that.

Ideally, you should pass in the lengths if you can. Recalculating
this stuff takes time, so if you have the information already you
should provide it.

But if you must do it without knowing in advance how long your
strings are, I''d do it much the same way you did:

#include <assert.h>
#include <string.h>

int ends_with(const char *name, const char *has)
{
int rc = 0; /* 0 means "no", 1 means "yes" */
size_t name_len;
size_t has_len;

assert(name != NULL);
assert(has != NULL);

name_len = strlen(name);
has_len = strlen(has);

if(name_len >= has_len)
{
name += name_len;
name -= has_len;
if(strcmp(name, has) == 0)
{
rc = 1;
}
}

return rc;
}

Alternatives using standard library functions are likely to be
clumsy. Consider, for example, strstr. You''d have to loop it
in case the substring occurred earlier in the string but not
at the end. And strrchr sounds like a good bet until you think
it through.

The pointer arithmetic idea you had was sound enough. Note my
uses of const, size_t, and assert. Also, I''ve simplified the
code so that each line just does one job rather than six. :-)


Andreas Klimas写道:
Andreas Klimas wrote:
你好,

有人知道标准功能还是更简单的方法
获得这个结果。

int ends_with(char * string,char * has);

回答字符串是否以子字符串结尾。

我的第一种方法:

int ends_with(char * name,char * has){
int name_len = strlen(name);
int has_len = strlen( has);
返回
name_len> = has_len&&
!memcmp(name + name_len-has_len,has,has_len);
}
祝福

Andreas Klimas
Hello,

does anybody know about a standardfunction or a simpler way
to get this result.

int ends_with(char *string, char *has);

answer whether string ends with substring has or not.
my first approach:

int ends_with(char *name, char *has) {
int name_len = strlen(name);
int has_len = strlen(has);
return
name_len>=has_len &&
!memcmp(name+name_len-has_len, has, has_len);
}

best wishes

Andreas Klimas



int ends_with(char * name,char * has){

int name_len = strlen(name);

int has_len = strlen(has);

返回名称+ name_len - has_len == has;


实际的memcmp()没有意义。如果地址相等,则字符串相等。

-

Joe Wright mailto:jo ******** @ comcast.net

所有东西都应尽可能简单,但不能简单。

---阿尔伯特爱因斯坦---



int ends_with(char *name, char *has) {
int name_len = strlen(name);
int has_len = strlen(has);
return name + name_len - has_len == has;

There is no point to the actual memcmp(). If the addresses are
equal, the strings are equal.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


Joe Wright< jo ******** @ comcast.net>写道:
Joe Wright <jo********@comcast.net> writes:
Andreas Klimas写道:
Andreas Klimas wrote:

有没有人知道标准函数或更简单的方法来获得这个结果。

int ends_with(char * string,char * has);

回答字符串是否以子字符串结尾。

does anybody know about a standardfunction or a simpler way
to get this result.

int ends_with(char *string, char *has);

answer whether string ends with substring has or not.



int ends_with(char * name,char * has){
int name_len = strlen(name);
int has_len = strlen(has);
返回名称+ name_len - has_len == has;
实际的memcmp()没有意义。如果地址相等,则字符串相等。


int ends_with(char *name, char *has) {
int name_len = strlen(name);
int has_len = strlen(has);
return name + name_len - has_len == has; There is no point to the actual memcmp(). If the addresses are
equal, the strings are equal.



Keh?

不适用于ends_with(" hello", lo),应该如此。

当然需要memcmp()或strcmp()。


-

Chris,


Keh?
Doesn''t work for ends_with("hello", "lo"), as it should.
Of course memcmp() or strcmp() will be required.

--
Chris,


这篇关于以。。结束() ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆