Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.4 kB
6
Indexable
Never
#include<stdio.h>
#include<string.h>
int type;
char input[100000];
char *ary;
char *str1,*str2,*str3,*token,*tmp;
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;
}

int main(){
    str1=malloc(10000*sizeof(char));
    str2=malloc(10000*sizeof(char));
    str3=malloc(10000*sizeof(char));
    ary=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;
    }
    else if(str3Len!=0 && strcmp(str3,"-i")!=0){
        type=3;
    }
    scanf("%[\n]");
    while(gets(ary)!=NULL){
        if(type==1){
            token=strstr(ary,str1);
            while(token){
                int pos=token-ary;
                ary[pos]='@';
                while(pos-1>=0 && (isalnum(ary[pos-1]) || ary[pos-1]=='-') ){
                    pos--;
                }
                int aryLen=strlen(ary);
                int i;
                for(i=pos;i<aryLen;i++){
                    if(ary[i]!='@' && !isalnum(ary[i]) && ary[i]!='-'){
                        break;
                    }
                    if(ary[i]!='@'){
                        printf("%c",ary[i]);
                    }
                    else{
                        printf("%s",str2);
                        i+=str1Len-1;
                    }
                }
                ary=&ary[i];
                token=strstr(ary,str1);
                printf("\n");
            }

        }
        else if(type==2){
            token=Strstr(ary,str1);
            while(token){
                int pos=token-ary;
                ary[pos]='@';
                while(pos-1>=0 && (isalnum(ary[pos-1]) || ary[pos-1]=='-') ){
                    pos--;
                }
                int aryLen=strlen(ary);
                int i;
                for(i=pos;i<aryLen;i++){
                    if(ary[i]!='@' && !isalnum(ary[i]) && ary[i]!='-'){
                        break;
                    }
                    if(ary[i]!='@'){
                        printf("%c",ary[i]);
                    }
                    else{
                        printf("%s",str2);
                        i+=str1Len-1;
                    }
                }
                ary=&ary[i];
                token=Strstr(ary,str1);
                printf("\n");
            }
        }
        else{
            printf("The input format: string1 string2 [parameter]\n");
            break;
        }
    }

}