Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

What is Ramayana, who is its creator, why was Ramayana written?

Answered: 1 week ago

Question

To solve by the graphical methods 2x +3y = 9 9x - 8y = 10

Answered: 1 week ago

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

=+How might you explain this phenomenon?

Answered: 1 week ago