这是来自K&R的练习5-4,我花了几个小时对它进行微调,但现在它似乎起作用了。我是新手,我欢迎任何关于如何做得更好的评论。
/* 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():
/* 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
发布于 2011-05-19 21:12:23
修正指针。这个函数没有改变内存中的任何东西,没有理由它们不应该是const。与您在函数中声明的变量相同;使它们为const
int strend(const char* s, const char* t)在输入指针中检查NULL。
尽可能使用库字符串函数。而不是做:
while(*s++) ;
s-=2;调用strlen(s);,然后根据需要在algo中减去1。由于您正在迭代字符串,所以用函数调用替换它具有相同的效率,并使其更具可读性。
在while循环周围添加{。这是未来的证明。
单元测试以确认正确性。
发布于 2011-05-20 00:12:28
我会改变它更像是..。
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/进行单元测试。(主要是因为我写的,虽然我有一个很晚的版本,但我还没有发布)
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));https://codereview.stackexchange.com/questions/2489
复制相似问题