Hi. I'm new to programming in C and I was trying to figure out how to make a program to read in a line of input that ends only when you press enter, but it has to use scanf() with %c. I know how to use %s but I need to know how to do it with %c as well. After the program reads in the input it's supposed to display what you entered backward. This is the code I have come up with, it works sometimes, but it tends to add extra characters to the array when you input longer strings:
I don't know how I figured out that while loop thing...I just messed with it until it worked... can anyone help me? I'm sure there's a simpler way to do this and maybe explain to me what's happening with this code? Thanks.Code:#include <stdio.h> int main(void) { char input[100]; int count = 0, length = 0; printf("Input your favorite phrase.\n"); while ( ((scanf("%c", &input[count]) - 1) + input[count]) != '\n') { count++; } length = strlen(input) - 1; for (count = length; count >= 0; count--) { printf("%c", input[count]); } getchar(); return 0; }
-Eric



LinkBack URL
About LinkBacks



