How do I read my name into an array and spit out my name in reverse order. I can not get this program to run
How do I read my name into an array and spit out my name in reverse order. I can not get this program to run
Last edited by steven; 09-06-2001 at 03:08 PM.
Code:#include <stdio.h> #define SIZE 50 int main(){ char array[SIZE]; int i; printf("Enter your name"); scanf("%s", array); for(i = SIZE - 1; i > 0; i--){ printf("%c", array[i]); } return 0; }
The above code assumes name will always be 50 characters long, and also first character stored in element 0 wouldnot be printed, would be better to include string.h and use
for(i = strlen(array) - 1; i >= 0; i--)
Last edited by bigtamscot; 09-07-2001 at 01:42 AM.
hoping to be certified (programming in c)
here's the news - I'm officially certified.
you could use strrev.
Code:#include<string.h> #include<stdio.h> int main() { char bud[255]; printf("Enter a string\n"); scanf("%s", bud); printf("\nString before reversal: %s\n\n", bud); strrev(bud); printf("\nString after reversal: %s\n", bud); return 0; }
That's true bigtamscot, though I was just posting a quick answer to get steven on his way, there are so many ways to do a simple thing in C that it's best to start at the bottom and become comfortable with each level of difficulty in succession.
WayTooHigh, I wasn't aware there was a strrev() function. You might want to check and see if that's unique to a single compiler or common to all of them, I couldn't find it on my copy of MSVC++.
-Prelude
My best code is written with the delete key.