Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.9 kB
9
Indexable
Never
#include<stdio.h>
#include<string.h>
#include<strings.h>
#include<stdbool.h>
#include <ctype.h>
#define PRINT(n) printf("%s", n)
char input[1000000];
char phase[100][100000];
char article[1000000];
int type;
char *token;
int article_L;
int phase1_L;
void findstr(int head, int tail){
    bool isFind=false;
    PRINT("the word is: ");
    for(int i=head;i<tail;i++){
        printf("%c",article[i]);
    }
    PRINT("\n");
    for(int i=head;i<=tail-phase1_L;i++){
        if(strncmp(&article[i], phase[0], phase1_L) == 0){
            isFind=true;
            article[i]='^';
            break;
        }
    }
    if(isFind){
        for(int i=head;i<tail;i++){
            if(article[i] == '^'){
                printf("%s",phase[1]);
                i += phase1_L-1;
            }
            else{
                printf("%c",article[i]);
            }
        }
        PRINT("\n");
    }
}
void findStr(int head, int tail){
    bool isFind=false;
    PRINT("the word is: ");
    for(int i=head;i<tail;i++){
        printf("%c",article[i]);
    }
    PRINT("\n");
    for(int i=head;i<=tail-phase1_L;i++){
        if(strncasecmp(&article[i], phase[0], phase1_L) == 0){
            isFind=true;
            article[i]='^';
            break;
        }
    }
    if(isFind){
        for(int i=head;i<tail;i++){
            if(article[i] == '^'){
                printf("%s",phase[1]);
                i += phase1_L-1;
            }
            else{
                printf("%c",article[i]);
            }
        }
        PRINT("\n");
    }
}
int main(){
    fgets(input, 1000000, stdin);
    token = strtok(input, " \r\n");
    int pos=0;
    while(token != NULL){
        strcpy(phase[pos++],token);
        token = strtok(NULL, " \r\n");
    }
    phase1_L=strlen(phase[0]);
    if(pos == 2){
        type = 1;
        PRINT("this is case 0\n");
    }
    else if(pos == 3 && (strcmp(phase[2], "-i") == 0) ){
        type = 2;
        PRINT("this is case 1\n");
    }
    else{
        type = 3;
        PRINT("The input format: string1 string2 [parameter]\n");
        return 0;
    }
    while(fgets(article, 1000000, stdin) != NULL){
        article_L = strlen(article);
        int head, tail;
        bool setHead = true;
        for (int i=0;i<article_L;i++){
            if(setHead && (isalnum(article[i]) || article[i]=='-')){
                head = i;
                setHead = false;
            }
            if(!(isalnum(article[i]) || article[i]=='-') && !setHead){
                setHead=true;
                tail = i;
                if(type == 1){
                    findstr(head, tail);
                }
                else{
                    findStr(head, tail);
                }
            }
        }
    }
}