打印字符串中最后一次出现 :: 之后的所有内容的应用 (C)

App that prints everything after the last occurrence of :: in a string ( C )

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


int isCH(char c) {
  return (c != ':' && c != '[=10=]');
}

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

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

改变这两行

  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");

更通用的功能。它将 return 指向最后一次出现任何定界符后剩余字符的指针。

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

另一种方法是从字符串的末尾开始。那么当第一对冒号被定位时,函数就可以return.

#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"));
}