【问题标题】:App that prints everything after the last occurrence of :: in a string ( C )在字符串 ( C ) 中最后一次出现 :: 之后打印所有内容的应用程序
【发布时间】:2021-02-22 16:05:21
【问题描述】:

我正在尝试编写一个在最后一次出现 2 个冒号“::”之后打印所有内容的应用程序,因此如果它是 3 个“:::”,则不会计算在内。如果字符串是“He::ll::o”,它应该打印出“o”,如果它是“12312::233”,它应该打印出“233”,我必须使用char* extract(char* input);和一个指针函数@987654322 @我今天刚开始学习C,有点不知所措。我也不允许导入库。这就是我到目前为止所拥有的。任何帮助表示赞赏。


int isCH(char c) {
  return (c != ':' && c != '\0');
}

char *extract(char *input){
    int state = 1;
    char *doubleColon;
    
    while(1){
        switch(state){
            case 1:
                if(isCH(*input))
                state = 1;
                if(*input == '\0')
                state = 2;
                if(*input == ':') // first colon
                state = 3;
            break;
            case 2:
                return doubleColon;
            break;
            case 3:
                if(isCH(*input))
                state = 1;
                if(*input == '\0')
                state = 2;
                if(*input == ':'){ // second colon
                state = 4;
                doubleColon = input;
                }
            break;
            case 4:
                if(isCH(*input))
                state = 1;
                if(*input == '\0')
                state = 2;
                if(*input == ':')
                state = 1;
                break;
        }
        input++;
    }
}

int main() {
    printf("End of String: %c", *extract("Ha::ll::o"));
} 

【问题讨论】:

  • extract 不是returning 任何东西,您的printf 调用应使用%s 格式说明符。
  • 只是一个样式说明,如果您为您的州创建一个枚举,这样它们会有一个名称而不是数字,它会看起来更好。

标签: c while-loop count switch-statement printf


【解决方案1】:

更通用的功能。它将在最后一次出现任何分隔符后返回指向剩余字符的指针。

char *extract(char *input, const char *del)
{
    char *last = NULL, *current = NULL;
    size_t delLength = strlen(del);

    if(input && del && *del)
    {
        do
        {
            if(current)
            {
                last = current + delLength;
                input = last;
            }
        }while((current = strstr(input, del)));
    }
    return last;
}

int main() {
    char *res;
    printf("End of String: %s\n", (res = extract("Ha::ll::o", "::")) ? res : "Not found");
    printf("End of String: %s\n", (res = extract("Ha::ll::o", ":ll:")) ? res : "Not found");
    printf("End of String: %s\n", (res = extract("", "::")) ? res : "Not found");
    printf("End of String: %s\n", (res = extract(NULL, "::")) ? res : "Not found");
} 

https://godbolt.org/z/EbM1cP

【讨论】:

    【解决方案2】:

    另一种方法是从字符串的末尾移到开头。那么当第一对冒号被定位时,函数就可以返回了。

    #include <stdio.h>
    #include <string.h>
    
    char *extract(char *input){
        char *doubleColon = NULL;
        char *end = input;
        int span = 0;
    
        //advence to end of string
        while ( *end) {
            ++end;
        }
    
        while ( end > input){
            --end;//work toward start of string
            if ( ':' == *end) {//found a colon
                doubleColon = end + 1;//set pointer
                span = 0;
                while ( ':' == *end) {//count consecutive colons
                    ++span;
                    if ( end == input && 2 == span) {
                        return doubleColon;
                    }
                     if ( end == input) {
                        return NULL;
                    }
                   --end;
                }
                if ( 2 == span) {//two consecutive colons
                    return doubleColon;
                }
                doubleColon = NULL;//reset pointer
            }
        }
        return doubleColon;
    }
    
    int main ( void) {
        printf ( "End of String: %s\n", extract ( "::Ha:::::ll:::o"));
        printf ( "End of String: %s\n", extract ( "::Ha::ll:::o"));
        printf ( "End of String: %s\n", extract ( "::Ha::ll::o"));
    }
    

    【讨论】:

      【解决方案3】:

      改变这两行

      1. doubleColon = input;doubleColon = input+1;
      2. printf("End of String: %c", *extract("Ha::ll::o"));printf("End of String: %s", extract("Ha::ll::o"));

      如果输入中没有::,还要检查大小写

      char *str = NULL;// in main function you have to do this.
      if(str = extract("Ha::ll::o"))
          printf("End of String: %s", str);
      else
          printf("No :: present");
      

      【讨论】:

      • extract 没有 return 任何东西。
      • case 2 返回
      • 我错过了。不过,在每种情况下或最后都没有return 是不好的做法。
      • 为什么我在输入中加了+1?在 doubleColon = input+1;
      • 我说我们需要添加它。您是否检查了输出而不添加?没有它,您将在输出中获得:
      猜你喜欢
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 2015-05-11
      • 2011-03-28
      • 1970-01-01
      • 2014-07-28
      相关资源
      最近更新 更多