Question
Can any experts fix my code please!?! #include #include #include void remove_new_line(char* str); int main(int argc, char* argv[]) { if(argc 3) { printf(Too many arguments
Can any experts fix my code please!?!
#include #include #include
void remove_new_line(char* str);
int main(int argc, char* argv[]) { if(argc <= 1) { printf("No arguments "); return 0; } else if(argc < 3) { printf("Not enough arguments "); return 0; } else if(argc > 3) { printf("Too many arguments "); return 0; } if(strlen(argv[1]) != strlen(argv[2])) { printf("Invalid arguments "); return 0; } else if(strlen(argv[1]) == strlen(argv[2])) { char input[100]; char* token; char* temp[100]; int i; fgets(input, sizeof(input), stdin); remove_new_line(input); token = strtok(input, " "); while(token != NULL) { temp[i++] = token; token = strtok(NULL, " "); } for(int j =0; j < i; j++) { if(strcat(temp[j], argv[1])) { temp[j] = argv[2]; printf("%s ", temp[j]); } } } }
void remove_new_line(char* str) { char* p; if (p = strchr(str, ' ')) { *p = '\0'; } }
The purpose is to replace letters in the user input from argv[1] to argv[2].. can someone please fix this codes?
For example, if I compile it, ./tr.c abc def
and the input is,
./tr xy XY
xxyyxx Lorem ipsum dolor sit amet xxyyxx
the output should be:
XXYYXX Lorem ipsum dolor sit amet XXYYXX
and also, please explain me the purpose of using remove_new_line!
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started