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
Printable View
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
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--)
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