How to reverse print in C language?

To reverse output a string or array, you can use a loop to iterate through the elements and use a new index to decrement the position of the iteration. Below is an example code:

#include <stdio.h>
#include <string.h>

void reverseString(char* str) {
    int length = strlen(str);
    for (int i = length - 1; i >= 0; i--) {
        printf("%c", str[i]);
    }
    printf("\n");
}

int main() {
    char str[] = "Hello World";
    reverseString(str);
    return 0;
}

This sample code will output the string “Hello World” in reverse, which is “dlroW olleH”.

广告
Closing in 10 seconds
bannerAds