Advertisement
Guest User

Untitled

a guest
May 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5.  
  6. char* pre_process_fix(char argv[]){
  7.    char* str1=malloc(strlen(argv)*sizeof(char));
  8.    int j=0;
  9.    for(int i=0;argv[i]!='\0';i++){
  10.       if(argv[i]=='\\' && i+1<strlen(argv)){
  11.          switch (argv[i+1]){
  12.             case 'n': str1[j++]='\n';i++;break;
  13.             case 't': str1[j++]='\t';i++;break;
  14.          }
  15.       }
  16.       else str1[j++]=argv[i];
  17.    }
  18.    str1[j++]='\0';
  19.    return realloc(str1,j*sizeof(char));
  20. }
  21.  
  22. char* pre_process_filler(char argv[]){
  23.    if(strlen(argv)==3 && argv[1]=='-'){
  24.       char *str1 = malloc( (argv[2] - argv[0]) * sizeof(char) );
  25.       int v=argv[0];
  26.       for(int i=0 ; v <= argv[2]; i++){
  27.          str1[i]=v++;
  28.       }
  29.       argv=str1;
  30.    }
  31.    return argv;
  32. }
  33.  
  34. char* pre_process_complete_str2(char argv1[],char argv2[]){
  35.    if(strlen(argv1) > strlen(argv2)){
  36.       int i;
  37.       char *temp = malloc(strlen(argv1)*sizeof(char));
  38.      
  39.       for(i=0; argv2[i]!='\0'; i++){
  40.          temp[i]=argv2[i];
  41.       }
  42.      
  43.       for(; i<strlen(argv1);i++){
  44.          temp[i]=temp[i-1];
  45.       }
  46.      
  47.       argv2=temp;
  48.    }
  49.    return argv2;
  50. }
  51.  
  52. void rep_print(int argc,char *argv[]){
  53.    argv[2] = pre_process_fix(argv[2]);
  54.    argv[2] = pre_process_filler(argv[2]);
  55.    
  56.    if(argc==4){
  57.       argv[3] = pre_process_fix(argv[3]);
  58.       argv[3] = pre_process_filler(argv[3]);
  59.       argv[3] = pre_process_complete_str2(argv[2],argv[3]);
  60.      
  61.       char c,rep=-1,subs=-1;
  62.       int found,rep_found=0;
  63.       while((c=getchar())!=EOF){
  64.          found=-1;
  65.          if(c==subs) found=1;
  66.          for(int i=0;argv[2][i]!='\0';i++){
  67.             if(c==argv[2][i]){
  68.                subs=argv[3][i];
  69.                found=i;
  70.                break;
  71.             }
  72.          }
  73.          if(found!=-1){
  74.             if(c!=rep&&rep_found!=1&&c!=subs) {
  75.                putchar(subs);
  76.                rep=c;
  77.                rep_found=1;
  78.             }
  79.          }
  80.          else {
  81.             putchar(c);
  82.             rep=c;
  83.             rep_found=0;
  84.             subs=-1;
  85.          }  
  86.       }
  87.       exit(0);
  88.    }
  89.    
  90.    char c,rep=-1;
  91.    int found=0;
  92.    while((c=getchar())!=EOF){
  93.       found=0;
  94.       for(int i=0;argv[2][i]!='\0';i++){
  95.          if(c==argv[2][i]) found=1;
  96.       }
  97.       if(found==1&&c==rep) rep=c;
  98.       else {
  99.          putchar(c);
  100.          rep=c;
  101.       }
  102.    }
  103.    exit(0);
  104. }
  105.  
  106. void del_print(char argv[]){
  107.    argv = pre_process_fix(argv);
  108.    argv = pre_process_filler(argv);
  109.    
  110.    int found=0;
  111.    char c;
  112.    while((c=getchar())!=EOF){
  113.       found=0;
  114.       for(int i=0;argv[i]!='\0';i++){
  115.          if(c==argv[i]){
  116.             found=1;
  117.             break;
  118.          }
  119.       }
  120.       if(found==0) putchar(c);
  121.    }
  122.    exit(0);
  123. }
  124.  
  125. void def_print (int argc, char *argv[]){
  126.    argv[1] = pre_process_fix(argv[1]);
  127.    argv[2] = pre_process_fix(argv[2]);
  128.    argv[1] = pre_process_filler(argv[1]);
  129.    argv[2] = pre_process_filler(argv[2]);
  130.    argv[2] = pre_process_complete_str2(argv[1],argv[2]);
  131.    
  132.    int found=0;
  133.    char c;
  134.    while((c=getchar())!=EOF){
  135.       found=0;
  136.       for(int i=0;argv[1][i]!='\0';i++){
  137.          if(c==argv[1][i]){
  138.             putchar(argv[2][i]);
  139.             found=1;
  140.             break;
  141.          }
  142.       }
  143.       if(found==0) putchar(c);
  144.    }
  145.    exit(0);
  146. }
  147.  
  148. int main(int argc, char *argv[]){
  149.    int c;
  150.    while((c = getopt(argc,argv,"sd:"))!=-1){
  151.       switch(c){
  152.          case 's': rep_print(argc,argv);
  153.          case 'd': del_print(optarg);
  154.       }
  155.    }
  156.    def_print(argc,argv);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement