首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查字符串在c中是否具有给定的后缀

检查字符串在c中是否具有给定的后缀
EN

Stack Overflow用户
提问于 2021-04-17 01:32:47
回答 2查看 70关注 0票数 0

如果你能帮我解决下面的问题,我将非常感激。我正在尝试查看字符串是否与后缀匹配。似乎一切都在工作,但没有得到预期的结果。尤其是第一次测试,根据解决方案函数中调试的*str == *suffix == '\0'语句,奇怪为什么打印结果为false

代码语言:javascript
复制
#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");
    }
}
EN

回答 2

Stack Overflow用户

发布于 2021-04-17 05:00:35

为了比较循环中的字符串末尾'\0',您必须克服的最大障碍是在测试suffixending之前不要在*str'\0' (例如0)时退出循环。

当您需要完全评估循环中通常用于控制循环退出的条件时,如何处理这些情况?连续循环,并将退出条件移动到循环体中--在进行了所需的检查之后。在您的情况下,您可以这样做:

代码语言:javascript
复制
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之后,如果strsuffix都在末尾,则进行比较。如果它们都在字符串的末尾,则返回true。退出条件是else if()检查str是否已到达末尾--如果已到达末尾,则循环中断并返回false

完整的示例如下:

代码语言:javascript
复制
#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))

示例使用/输出

代码语言:javascript
复制
$ ./bin/suffix_ending
Success
Success
Success

仔细检查一下,如果你还有其他问题,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2021-04-17 14:00:53

OP的solution(const char *string, const char *ending) fails用于在部分匹配后正确地重新同步。

一种更好的方法:

代码语言:javascript
复制
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

使用*str == *suffix && *str == '\0'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67129758

复制
相关文章

相似问题

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