Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

argvtac .c is a program that concatenates in reverse order all the arguments passed to it as command line arguments and prints to standard output

argvtac.c is a program that concatenates in reverse order all the arguments passed to it as command line arguments and prints to standard output the resulting string. The program has correct output, but leaks memory. Run valgrind in the your code-server IDE to find and correct the leak and submit the corrected code.

For example:

Test

Result

./argvtac

./argvtac

./argvtac

./argvtac

./argvtac mb

mb./argvtac

#include #include #include

/* return the length of a string * For example, * return 0 if s is \"\" * return 3 if s is \"abc\" */ unsigned long my_strlen(char *s) { unsigned long i = 0;

while (*s++) i++; return i; }

/* Concatnate two strings. * Dynamically allocate space for the result. * Return the address of the result. */ char *my_strcat(char *s1, char *s2) { char *r; unsigned long len1, len2, i;

len1 = my_strlen(s1); len2 = my_strlen(s2);

r = malloc(len1 + len2 + 1); assert (r != NULL);

// copy, using array notation for(i = 0; i r[i] = s1[i];

for(i = 0; i r[len1+i] = s2[i];

r[len1+len2] = 0; return r; }

/* * Concatenate command line arguments in reverse order * and print them to the standard output */ int main(int argc, char *argv[]) { int i; char *s;

// initialize s to be \"\" s = malloc(1); assert(s != NULL); *s = 0;

for(i = argc-1; i >= 0; i--) s = my_strcat(s, argv[i]);

printf(\"%s \", s);

return 0; }

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

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions