簡體   English   中英

在字符串 ( 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 != '\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"));
} 

更通用的功能。 在最后一次出現任何分隔符后,它將返回指向剩余字符的指針。

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

改變這兩行

  1. doubleColon = input; to doubleColon = input+1;
  2. printf("End of String: %c", *extract("Ha::ll::o")); to 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");

另一種方法是走到字符串的末尾並從頭開始。 然后當第一對冒號被定位時,函數可以返回。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM