首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确定一个字符串是否发生在另一个字符串的末尾

确定一个字符串是否发生在另一个字符串的末尾
EN

Code Review用户
提问于 2011-05-19 20:30:56
回答 2查看 401关注 0票数 7

这是来自K&R的练习5-4,我花了几个小时对它进行微调,但现在它似乎起作用了。我是新手,我欢迎任何关于如何做得更好的评论。

代码语言:javascript
复制
/* Function strend(s, t), which returns 1 if the string t 
 * occurs at the end of string s and zero otherwise */
int strend(const char *s, const char *t)
{   
    const char *s0, *t0;

    if (s == 0) {
        printf("s is NULL pointer\n");
        return (-1);
    }
    if (t == 0) {
        printf("t is NULL pointer\n");
        return (-1);
    }
    s0 = s;
    t0 = t;

    while (*s++)
        ;
    s -= 2;         /* *s points to last real char in s */

    while (*t++)
        ;
    t -= 2;         /* *t points to last real char in t */

    if ((t-t0) > (s-s0))
            return (0);  /* t is longer than s */

    while (t>=t0) {
        if (*s-- != *t--) 
            return (0);  /* Mismatch */
    }

    return (1); /* Match */
}

以下是main()

代码语言:javascript
复制
/* Test the function strend(s, t), which returns 1 if the string t 
 * occurs at the end of string s and zero otherwise */
#include "jim.h"
#include "subs.c"

char a[MAXLINE], b[MAXLINE];

int main (void)

{
    printf("Return = %1d, Expect = 1\n", strend("12345", "45"));
    printf("Return = %1d, Expect = 0\n", strend("12345", "35"));
    printf("Return = %1d, Expect = 0\n", strend("45", "345"));
    printf("Return = %1d, Expect = 1\n", strend("12345", "12345"));
    printf("Return = %1d, Expect = 1\n", strend("12345", "5"));
    printf("Return = %1d, Expect = 0\n", strend("12345", "4"));
    printf("Return = %1d, Expect = 1\n", strend("12345", ""));
    printf("Return = %1d, Expect = 0\n", strend("12345", "+"));
    printf("Return = %1d, Expect = 0\n", strend("12345", "a2345"));
    printf("Return = %1d, Expect = 1\n", strend("", ""));
    printf("Return = %1d, Expect = 0\n", strend("", "Z"));
    printf("Return = %1d, Expect = 1\n", strend("1", "1"));
    printf("Return = %1d, Expect = 0\n", strend("1", "1A"));
    printf("Return = %1d, Expect = -1\n", strend(0, "1A"));
    printf("Return = %1d, Expect = -1\n", strend("1", 0));
}

这是输出:

返回= 1,Expect =1返回= 0,Expect =0返回= 0,Expect =0返回= 1,Expect =1返回= 1,Expect =1返回= 1,Expect =1返回= 0,Expect =0返回= 0,Expect =0返回= 1,Expect =0返回= 1,Expect =1返回= 0,Expect =0是空指针返回= -1,Expect = -1 t是空指针返回= -1,Expect = -1

EN

回答 2

Code Review用户

回答已采纳

发布于 2011-05-19 21:12:23

修正指针。这个函数没有改变内存中的任何东西,没有理由它们不应该是const。与您在函数中声明的变量相同;使它们为const

代码语言:javascript
复制
int strend(const char* s, const char* t)

在输入指针中检查NULL。

尽可能使用库字符串函数。而不是做:

代码语言:javascript
复制
while(*s++) ;
s-=2;

调用strlen(s);,然后根据需要在algo中减去1。由于您正在迭代字符串,所以用函数调用替换它具有相同的效率,并使其更具可读性。

在while循环周围添加{。这是未来的证明。

单元测试以确认正确性。

票数 6
EN

Code Review用户

发布于 2011-05-20 00:12:28

我会改变它更像是..。

代码语言:javascript
复制
char* start_of_s = s;
char* start_of_t = t;
if(s == NULL || t==NULL) return 0;
while(*s) s++;
while(*t) t++;
while(*s == *t && s > start_of_s && t> start_of_t)
{
    s--; t--;
}
return (t == start_of_t) && (*s == *t);

此外,单元测试与您的测试(和我的),除了我标出了"string_is_at_end“。我使用http://code.google.com/p/seatest/进行单元测试。(主要是因为我写的,虽然我有一个很晚的版本,但我还没有发布)

代码语言:javascript
复制
    assert_true(string_is_at_end("12345",""));
    assert_true(string_is_at_end("123","23"));
    assert_true(string_is_at_end("123","3"));
    assert_false(string_is_at_end("","123"));
    assert_false(string_is_at_end("123","1"));
    assert_false(string_is_at_end("123","2"));
    assert_true(string_is_at_end("12345", "45"));
    assert_false(string_is_at_end("12345", "35"));
    assert_false(string_is_at_end("45", "345"));
    assert_true(string_is_at_end("12345", "12345"));
    assert_true(string_is_at_end("12345", "5"));
    assert_false(string_is_at_end("12345", "4"));
    assert_true(string_is_at_end("12345", ""));
    assert_false(string_is_at_end("12345", "+"));
    assert_false(string_is_at_end("12345", "a2345"));
    assert_true(string_is_at_end("", ""));
    assert_false(string_is_at_end("", "Z"));
    assert_true(string_is_at_end("1", "1"));
    assert_false(string_is_at_end("1", "1A"));
    assert_false(string_is_at_end(0, "1A"));
    assert_false(string_is_at_end("1", 0));
票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/2489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档