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

前端 未结 6 1101
日久生厌
日久生厌 2021-01-16 12:15

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

6条回答
  •  半阙折子戏
    2021-01-16 13:10

    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.

提交回复
热议问题