Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.7 kB
8
Indexable
Never
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int position[1000000];
int posCount=0;
char *cmpStr="- ";
char *Strstr(char *a,const char *b){
    int len_b = strlen(b);
    while(*a){
        if(strncasecmp(a,b,len_b)==0){
            return a;
        }
        a++;
    }
    return NULL;
}

char *str_replace (char *source, char *find,  char *rep, bool isI){
    int find_L=strlen(find);
    int rep_L=strlen(rep);
    int length=strlen(source)+1;
    int gap=0;
    char *result = (char*)malloc(sizeof(char) * length);
    strcpy(result, source);
    char *former=source;
    char *location;
    if(isI){
        location= Strstr(former, find);
    }
    else{
        location= strstr(former, find);
    }
    while(location!=NULL){
        gap+=(location - former);
        position[posCount++]=gap;
        result[gap]='\0';
        length+=(rep_L-find_L);
        result = (char*)realloc(result, length * sizeof(char));
        strcat(result, rep);
        gap+=rep_L;
        former=location+find_L;
        strcat(result, former);
        if(isI){
            location= Strstr(former, find);
        }
        else{
            location= strstr(former, find);
        }
    }
    return result;
}

char *str1,*str2,*str3,*token;


char ary[10000000];
int type;
char input[100000];
int main(){
    str1=malloc(10000*sizeof(char));
    str2=malloc(10000*sizeof(char));
    str3=malloc(10000*sizeof(char));
    memset(str1,0,sizeof(0));
    memset(str2,0,sizeof(0));
    memset(str3,0,sizeof(0));
    gets(input);
    token=strtok(input, cmpStr);
    if(token!= NULL ) {
      strcpy(str1,token);
      token=strtok(NULL, cmpStr);
    }
    if(token!= NULL ) {
      strcpy(str2,token);
      token=strtok(NULL, cmpStr);
    }
    if(token!= NULL ) {
      strcpy(str3,token);
      token=strtok(NULL, cmpStr);
    }
    //printf("%s\n%s\n%s\n",str1,str2,str3);
    //printf("%d\n%d\n%d\n",strlen(str1),strlen(str2),strlen(str3));
    int str1Len=strlen(str1);
    int str2Len=strlen(str2);
    int str3Len=strlen(str3);
    
    if(token!= NULL){
        type=3;
    }
    else if(str2Len==0){
        type=3;
    }
    else if(str3Len==0){
        type=1;
    }
    else if(strcmp(str3,"i")==0){
        type=2;
    }
    
    int aryLen;
    char *ans;

    while(gets(ary)!=NULL){

        if(type==1){
            memset(position,0,sizeof(position));
            posCount=0;
            ans=str_replace(ary,str1,str2,false);
            int pre=-1000;
            for(int i=0;i<posCount;i++){
                int j=position[i];
                while(j-1>=0 && (isalnum(ans[j-1]) || ans[j-1]=='-') ){
                    j--;
                }
                if(pre==j){
                    continue;
                }
                pre=j;
                char *tmp=&ans[j];
                sscanf(tmp,"%[a-zA-Z0-9-]",tmp);
                printf("%s\n",tmp);
            }
        }
        else if(type==2){
            memset(position,0,sizeof(position));
            posCount=0;
            ans=str_replace(ary,str1,str2,true);
            int pre=-1000;
            for(int i=0;i<posCount;i++){
                int j=position[i];
                while(j-1>=0 && (isalnum(ans[j-1]) || ans[j-1]=='-') ){
                    j--;
                }
                if(pre==j){
                    continue;
                }
                pre=j;
                char *tmp=&ans[j];
                sscanf(tmp,"%[a-zA-Z0-9-]",tmp);
                printf("%s\n",tmp);
            }
        }
        else{
            printf("The input format: string1 string2 [parameter]\n");
        }

    }

}



/*

he it
g thedfhe he

*/