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

前端 未结 6 1045
日久生厌
日久生厌 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:00

    //Change end to int type and modify your for loop as shown.
    #include 
    #include 
    
    int main(void) 
    {
    
    char word[50];
    int end;
    char x;
    
    printf("Enter a word and I'll give it to you backwards: ");
    
    scanf("%s", word);
    
    end = strlen(word) - 1;
    
     for (x = end; x >= 0; x--) 
     printf("%c",word[x] );
    
    
    return 0;
    }
    

提交回复
热议问题