I need to take anything that is given at a prompt, text or numbers, and put it in a string. Then the program returns whatever was typed in exactly backwards. Example Hello World would come out dlroW olleH. Any help would be much appreicated.
I need to take anything that is given at a prompt, text or numbers, and put it in a string. Then the program returns whatever was typed in exactly backwards. Example Hello World would come out dlroW olleH. Any help would be much appreicated.
We do not do homework here. Put some effort into it. Post your code. (IE: Try it on your own.)
Quzah.
Hope is the first step on the road to disappointment.
I wasn't asking for someone to do it for me, I just wanted some suggestions. Here's what I have so far:
#include <stdio.h>
#define SIZE 1024
int main()
{
char name[SIZE];
printf("Enter input: ");
gets(name); //The gets function reads 1 line from standard input
return 0;
}
The only thing I can come up with is to use strlen() in some way. Thanks for any help
Ok, here's what you need to do:
1) never ever use 'gets'. Use fgets instead. It is safer.
2) Use a loop, starting at the last character in your name and counting backwards, displaying each letter in the name.
To get the name length, use the strlen function.
Here is a fun way:
Quzah.Code:void r( void ) { int c = fgetc(stdin); if( c != '\n' ) r( ); printf("%c", c ); } int main ( void ) { printf("Enter a string: "); r( ); return 0; }
Hope is the first step on the road to disappointment.
>Here is a fun way:
But you're not allowed to have fun according to most teachers, it's very serious business.
Then again, most teachers are idiots.
Here is a more conventional way:
[highly pedantic]Code:void reverse ( char *a ) { int temp, x, y = strlen ( a ) - 1; for ( x = 0; x < y; x++, y-- ) temp = a[x], a[x] = a[y], a[y] = temp; }
>int main()
When declaring main to take no parameters in C, it's acceptable to leave it empty, but better to explicitly say void:
int main ( void )
[/highly pedantic]
>//The gets function reads 1 line from standard input
Correct, now tell me what gets does to make sure that it doesn't read more than your array can hold.
-Prelude
My best code is written with the delete key.
Maybe just use the strrev function, if you got it.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
>Maybe just use the strrev function, if you got it.
Or not, for three reasons:
1) strrev is not a standard function, so he may not have it.
2) This reeks of homework, in which case it must be done the hard way.
3) I just like disagreeing with people for some reason.
-Prelude
My best code is written with the delete key.