My head is getting twisted trying to figure this out.
The assignment is to get a line of text from the user and then print it out in reverse including the Hex code for each character.
I thought it would have been simple, and probably is once I understand the usage.
My thoughts were, I would set up my variable using char stg_array[80] whuch would give me a max size of 80 char.
I would then do a
while( (stg_array[i] = getch() ) != '\n' )
{
i++;
}
Then I would use strlen() to determine the actual length (I realize I already have it, just want to use strlen() for practice) and drop into a for loop that would countdown from the strlen(stg_array)-1 and
printf("Array[%d] = %s Hex Value = %X", i, stg_array[i], i );
I understand the buffer overflow bug I could have if the user types more than 80 chars. Not worried about that right now.
What I’m getting is the strlen() is returning the length to the first space in the text.
If I skip the user input and just make char stg_array[80]={"The quick brown fox."} with a fixed for loop in the middle, say 10, the program crashes.
I’m thinking I’m out to lunch about how C string arrays function.
Hope the following helps!
Output: