Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <getopt.h>
- char* pre_process_fix(char argv[]){
- char* str1=malloc(strlen(argv)*sizeof(char));
- int j=0;
- for(int i=0;argv[i]!='\0';i++){
- if(argv[i]=='\\' && i+1<strlen(argv)){
- switch (argv[i+1]){
- case 'n': str1[j++]='\n';i++;break;
- case 't': str1[j++]='\t';i++;break;
- }
- }
- else str1[j++]=argv[i];
- }
- str1[j++]='\0';
- return realloc(str1,j*sizeof(char));
- }
- char* pre_process_filler(char argv[]){
- if(strlen(argv)==3 && argv[1]=='-'){
- char *str1 = malloc( (argv[2] - argv[0]) * sizeof(char) );
- int v=argv[0];
- for(int i=0 ; v <= argv[2]; i++){
- str1[i]=v++;
- }
- argv=str1;
- }
- return argv;
- }
- char* pre_process_complete_str2(char argv1[],char argv2[]){
- if(strlen(argv1) > strlen(argv2)){
- int i;
- char *temp = malloc(strlen(argv1)*sizeof(char));
- for(i=0; argv2[i]!='\0'; i++){
- temp[i]=argv2[i];
- }
- for(; i<strlen(argv1);i++){
- temp[i]=temp[i-1];
- }
- argv2=temp;
- }
- return argv2;
- }
- void rep_print(int argc,char *argv[]){
- argv[2] = pre_process_fix(argv[2]);
- argv[2] = pre_process_filler(argv[2]);
- if(argc==4){
- argv[3] = pre_process_fix(argv[3]);
- argv[3] = pre_process_filler(argv[3]);
- argv[3] = pre_process_complete_str2(argv[2],argv[3]);
- char c,rep=-1,subs=-1;
- int found,rep_found=0;
- while((c=getchar())!=EOF){
- found=-1;
- if(c==subs) found=1;
- for(int i=0;argv[2][i]!='\0';i++){
- if(c==argv[2][i]){
- subs=argv[3][i];
- found=i;
- break;
- }
- }
- if(found!=-1){
- if(c!=rep&&rep_found!=1&&c!=subs) {
- putchar(subs);
- rep=c;
- rep_found=1;
- }
- }
- else {
- putchar(c);
- rep=c;
- rep_found=0;
- subs=-1;
- }
- }
- exit(0);
- }
- char c,rep=-1;
- int found=0;
- while((c=getchar())!=EOF){
- found=0;
- for(int i=0;argv[2][i]!='\0';i++){
- if(c==argv[2][i]) found=1;
- }
- if(found==1&&c==rep) rep=c;
- else {
- putchar(c);
- rep=c;
- }
- }
- exit(0);
- }
- void del_print(char argv[]){
- argv = pre_process_fix(argv);
- argv = pre_process_filler(argv);
- int found=0;
- char c;
- while((c=getchar())!=EOF){
- found=0;
- for(int i=0;argv[i]!='\0';i++){
- if(c==argv[i]){
- found=1;
- break;
- }
- }
- if(found==0) putchar(c);
- }
- exit(0);
- }
- void def_print (int argc, char *argv[]){
- argv[1] = pre_process_fix(argv[1]);
- argv[2] = pre_process_fix(argv[2]);
- argv[1] = pre_process_filler(argv[1]);
- argv[2] = pre_process_filler(argv[2]);
- argv[2] = pre_process_complete_str2(argv[1],argv[2]);
- int found=0;
- char c;
- while((c=getchar())!=EOF){
- found=0;
- for(int i=0;argv[1][i]!='\0';i++){
- if(c==argv[1][i]){
- putchar(argv[2][i]);
- found=1;
- break;
- }
- }
- if(found==0) putchar(c);
- }
- exit(0);
- }
- int main(int argc, char *argv[]){
- int c;
- while((c = getopt(argc,argv,"sd:"))!=-1){
- switch(c){
- case 's': rep_print(argc,argv);
- case 'd': del_print(optarg);
- }
- }
- def_print(argc,argv);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement