How to print an entered string backwards in C using only a for loop

33,437

Solution 1

What you have loops between the array element values. You want to loop between the array indexes. Update your loop to the following:

for (x = end; x >= 0; --x) {
    printf("%c", word[x]);
}

Note that this goes from the last index to zero and output the character at that index. Also a micro-optimization in the for loop using pre-decrement.

Solution 2

You're calling array values and not the specific index.

for(x = end; x >= 0; x--) { printf("%c", word[x]); }

Solution 3

You want to print word[x] (the xth character in the array) instead of x (the xth character in the character set).

You also want to be counting down indexes, not characters.

for(x=end, x >= 0; x--)
    printf("%c", word[x]);
Share:
33,437
Corey
Author by

Corey

Updated on July 10, 2022

Comments

  • Corey
    Corey almost 2 years

    I want to print a string backwards. But my code seems to count down the alphabet from the last letter in the array to the first letter in the array instead of counting down the array itself and spitting out each letter in the array.

    My code,

       #include <stdio.h>
       #include <string.h>
    
       int main(void) {
    
    char word[50];
    char end;
    char x;
    
    printf("Enter a word and I'll give it to you backwards: ");
    
    scanf("%s", word);
    
    end = strlen(word) - 1;
    
    for (x = word[end]; x >= word[0]; x--) {
        printf("%c", x);
    }
    
    return 0;
    }
    

    Any suggestions? Thank you.

    • Jason Rogers
      Jason Rogers over 13 years
      your error is that your not getting the element in your array, you get the value of your last character (examples the letter u) then you decrement the value of that letter and print that (u, t, s, r...) that explains your reverse alphabet
    • Jason Rogers
      Jason Rogers over 13 years
      I was curious so I posted in a different question stackoverflow.com/questions/4331486/…. and there is a "different" way to loop through the array that doesn't involve a counter.
    • Nicholas Knight
      Nicholas Knight over 13 years
      Since you're here... please be sure to read up on the problems with scanf before using it in real code: stackoverflow.com/questions/2430303/disadvantages-of-scanf
  • Jason Rogers
    Jason Rogers over 13 years
    What is the point of the micro optimization here ? it isn't changing the evaluation of the for or the print? (I'm not criticizing, I just don't understand the difference of pre vs post increment in the case)
  • Corey
    Corey over 13 years
    Thanks Jason (McCreary)! I feel stupid being that close and not working through it! face palm -- could you elaborate on Jason's (Rogers) comment, I too do not see the difference in this case with using a pre-decrement. I changed the code and tested and saw no difference, does it just run through the loop an extra time unnecessarily?
  • Jason McCreary
    Jason McCreary over 13 years
    @Jason, this has to do with how the pre and post decrement operators compile. Pre compiles to decrement, store. Post compiles to read, decrement, store. Note the extra read step. Again, a micro optimization. But one that can be commonly used in loops as there is rarely an explicit need to read the value first. However, there may be a need in a statement like b = 1 + x--;.
  • Jason Rogers
    Jason Rogers over 13 years
    oooh ok. I know the difference in post and pre I just didn't see the point here, but I try to make it a point to understand the differences in codes (even if micro). FYI to all: the exact difference in between post and pre increment is a favorite question of recruiters ^^
  • Jason McCreary
    Jason McCreary over 13 years
    @Jason, indeed. But I consider my point a bit deeper. On the surface the difference is order of operation. pre updates the value before the rest of the expression, post does after. b = 1 + x--; and b = 1 + --x; will give you different results.
  • Jason Rogers
    Jason Rogers over 13 years
    yep I know, actually you can see that in the debugger, the debugger seems to go one extra step on the for statement.
  • Corey
    Corey over 13 years
    @JasonM So in your first example x will be decremented after 1 is added and the second example x will be decremented before 1 is added?
  • Jason McCreary
    Jason McCreary over 13 years
    @Corey, exactly. That's the difference in the operators. Because of which, pre is best to use in situations where it's not part of a larger evaluated expression. Again, a micro optimization.