Question: I have a simple program in C. The program has the following parameters: -It will read a single line of text from standard input. -It

I have a simple program in C.

The program has the following parameters:

-It will read a single line of text from standard input. -It will print "R=" followed by that line backwards and then exit. eg: ./linex 123456 would output R=654321 Notes: i) your program must not leak memory or perform any invalid memory accesses. ii) your program should not use more memory than it needs. iii) do not call fgets or getline

Without using fgets, scanf is the only option. With this I need to be able to read a single line and make sure that the memory used is not larger than needed. How would I use scanf to creae dynamic memory and also read the white spaces in which scanf finishes. Thankyou for your help.

Thus far I have the following code:

#include

#include

#include

char * strrev(char *str) {

long i = strlen(str) - 1, j = 0;

char ch;

while(i > j)

{

ch = str[i];

str[i] = str[j];

str[j] = ch;

i--;

j++;

}

return str;

}

int main(int argc, const char * argv[]) {

long a = 10;

char name[a];

scanf("%[^ ]s",name);

a = strlen(name);

printf("%s ", name);

printf("%s ", strrev(name));

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!