Write a program that inputs a line of text and uses stack object to print the line reversed.

I found this C question in:

http://abhaygoel.wordpress.com/c-dac-ent...

I have difficulty to solve it, anyone can help!

>I have difficulty to solve it
What have you tried? Saying you've had "difficulty" is pretty vague.

Get several sheets of paper, write one letter of a word on each one.
"push" the letters onto a "stack"
Then figure out how to get the letters back in reverse order to print them.

/*Read a line of text and write it out reversely */

#include<stdio.h>
#define EOF '\n'
void reverse(); 			/*Function prototype*/

main()
{
	printf("Enter a line of text:\n\n");
	reverse();
}

void reverse()
{
	char c;
	if((c = getchar())!=EOF) 
	reverse();
	putchar(c);
	return;
}

What any modification needed to be done?

well your main function should be

int main(){

  return 0;
}

Also EOF is a reserved macro. So call yours something else like "NEWLINE" or such.

getchar() returns an int, so that char c, needs to be int c

Code without proper tag it is frowned upon. Next time make sure you know how to do it. Example here.

>What any modification needed to be done?
While recursion could technically be described as a stack, I seriously doubt that's what your program requirements meant by "stack object". More likely, you're supposed to implement your own stack using an array of char.

>#define EOF '\n'
EOF is a standard name, as has already been said. There's also no reason to hide '\n' behind a macro. One might do so for two reasons:

1) The macro offers clarifying information such that a value can be more readily understood by readers. '\n' is a well known escape sequence, to the point that any reader who doesn't know what it means likely doesn't know C at all.

2) The macro offers a way to use a value such that the usage is always portable even when the value is not. '\n' is already portable across all implementations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.