如果你能帮我解决下面的问题,我将非常感激。我正在尝试查看字符串是否与后缀匹配。似乎一切都在工作,但没有得到预期的结果。尤其是第一次测试,根据解决方案函数中调试的*str == *suffix == '\0'语句,奇怪为什么打印结果为false
#include <stdio.h>
#include <stdbool.h>
bool solution(const char *string, const char *ending);
void test(bool actual, bool expected);
int main(int argc, char const *argv[])
{
test(true, solution("abc", "bc"));
test(false, solution("abc", "d"));
test(true, solution("abc", ""));
return 0;
}
bool solution(const char *string, const char *ending)
{
const char * str = string;
const char * suffix = ending;
while(*str) {
if (*str == *suffix) {
suffix++;
} else {
suffix = ending;
}
str++;
}
// print statemet just for debugging
printf("Before return, str: '%c', suffix: '%c', null: '%c'\n", *str, *suffix, '\0');
return *str == *suffix == '\0';
}
void test(bool actual, bool expected) {
if (actual == expected) {
printf("Success\n");
} else if (actual == true) {
printf("failure: expected true!\n");
} else {
printf("failure: expected false!\n");
}
}发布于 2021-04-17 05:00:35
为了比较循环中的字符串末尾'\0',您必须克服的最大障碍是在测试suffix和ending之前不要在*str为'\0' (例如0)时退出循环。
当您需要完全评估循环中通常用于控制循环退出的条件时,如何处理这些情况?连续循环,并将退出条件移动到循环体中--在进行了所需的检查之后。在您的情况下,您可以这样做:
bool solution (const char *string, const char *ending)
{
const char *str = string;
const char *suffix = ending;
for (;;) { /* loop continually to evaluate end of string */
if (*str == *suffix) {
suffix++;
}
else {
suffix = ending;
}
str++;
if (!*str && !*suffix) { /* if at end of both, return true */
return true;
}
else if (!*str) { /* if at end of str, break, returning false */
break;
}
}
return false;
}在递增str之后,如果str和suffix都在末尾,则进行比较。如果它们都在字符串的末尾,则返回true。退出条件是else if()检查str是否已到达末尾--如果已到达末尾,则循环中断并返回false。
完整的示例如下:
#include <stdio.h>
#include <stdbool.h>
bool solution (const char *string, const char *ending);
void test (bool actual, bool expected);
int main (void)
{
test (true, solution("abc", "bc"));
test (false, solution("abc", "d"));
test (true, solution("abc", ""));
return 0;
}
bool solution (const char *string, const char *ending)
{
const char *str = string;
const char *suffix = ending;
for (;;) { /* loop continually to evaluate end of string */
if (*str == *suffix) {
suffix++;
}
else {
suffix = ending;
}
str++;
if (!*str && !*suffix) { /* if at end of both, return true */
return true;
}
else if (!*str) { /* if at end of str, break, returning false */
break;
}
}
return false;
}
void test (bool actual, bool expected)
{
if (actual == expected) {
printf("Success\n");
}
else if (actual == true) {
printf("failure: expected true!\n");
}
else {
printf("failure: expected false!\n");
}
}(注意:您的程序不接受任何参数,因此main()的正确调用是int main (void))
示例使用/输出
$ ./bin/suffix_ending
Success
Success
Success仔细检查一下,如果你还有其他问题,请告诉我。
发布于 2021-04-17 14:00:53
OP的solution(const char *string, const char *ending) fails用于在部分匹配后正确地重新同步。
一种更好的方法:
bool solution(const char *string, const char *ending) {
size_t string_len = strlen(string);
size_t ending_len = strlen(ending);
if (string_len < ending_len) {
return false;
}
return memcmp(string + (string_len - ending_len), ending, ending_len) == 0;
}当使用*str == *suffix == '\0'时,
为什么为false
https://stackoverflow.com/questions/67129758
复制相似问题