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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started