Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can any experts fix my code please!?! #include #include #include void remove_new

ID: 3844566 • Letter: C

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 <= 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 = '';
}
}

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,

the output should be:

and also, please explain me the purpose of using remove_new_line!

Thanks!

Explanation / Answer

See Argv1 and v2 are basically character arrays, and thus a null character is necessary to avoid

like for example End of string is needed to be determined for this program.

because ABS in language would be stored as 'A' 'B''C''' , which would lead to wrong output if not eliminated. So , this metid is required..:)

Fixing your code:

#include <stdio.h>
#include <string.h>
#include <conio.h>

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 = '';
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote