Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can anyone fix this code? Test test_stropy_1 (./test_stropy - passed Test test_stropy_2 (/test_stropy a) - passed Test test_stropy_6 (/test_stropy test test what) - passed Test

can anyone fix this code?

image text in transcribedimage text in transcribedimage text in transcribed
Test test_stropy_1 (./test_stropy - passed Test test_stropy_2 (/test_stropy "a") - passed Test test_stropy_6 (/test_stropy "test test what") - passed Test test_stropy_4 ( /test_stropy "hey hey" "hey hey") - failed (Incorrect outpu t. ) Your program produced these 3 lines of output: Please enter 1 argument: Your string String 1: hey hey String 2: hey hey The correct 1 lines of output for this test were: Please enter 1 argument: Your string The difference between your output(-) and the correct output (+) is: Please enter 1 argument: Your string - String 1: hey hey String 2: hey hey Test test_stropy_5 ( /test_stropy "last test ") - passed 4 tests passed 1 tests failed1 #include 2 #include 3 #include QUIA char *copyString(char *str) ; 7 int main( int argc, char *argv ) 8 { 9 if (argc != 2) 10 11 printf ( "Please enter 1 argument: Your string\ ") ; 12 exit; 0; 13 14 15 char *argvCopy = copyString(argv[1] ) ; 16 printf ( "String 1: $5\\", argv[1]); 17 printf ( "String 2: $5\ ", argvCopy) ; 18 19 free (argvCopy) ; 20 argvCopy = NULL; 21 return 0; 22 } 23 24 char *copyString(char *str) 25 { 26 char *s; 27 int len; 28 29 len = strlen(str) ; 30 s = (char *) malloc (len+1) ; 31 if (S == NULL) 32 33 printf ( "malloc error\\") ; 34 exit; 0; 35 36 strcpy (s, str) ; 37 return s; 38 }Exercise 2: Malloc and Strcpy Modify the C program we've written strcpy.c which takes in a single command line argument, creates a copy of it using malloc, and then prints both. To create a copy of a string, you first need to malloc space for the new character array (don't forget to add an extra "+ 1" for the '\\0' terminator!). Once you have malloc'd this you then need to use strcpy to copy data from the old string into the new string. You can copy this file into your current directoy by running $ cp ~csl911/public_htm|/t|b/08/strcpy.c . l If no arguments are passed in, or more than 1 argument is passed in, then the program should execute the following code: printf("P1ease enter 1 argument: Your string\ "); exit a; Expected output includes: $ ./strcpy "Hello there" String 1: Hello there String 2: Hello there $ ./strcpy "Hello there" "Second argument" Please enter 1 argument: Your string If malloc fails at any point (due to not being able to allocate memory), please print the following error: malloc error Once you get the program functioning, make sure you have correctly used "free" function to free the memory, and then set the pointers to be equal to NULL (it's good practice). l $ 1911 autotest Iab08 strcpy.c j

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

6 What are the hierarchy of effects models?

Answered: 1 week ago