Index « Previous Next »

Question

Write a program that prompts the user to input a string and display the string from backward.

Source Code

#include <stdio.h>

int main()
{
    char str[80];
    int l, i;

    printf("Enter a string: ");
    gets(str);

    /* Find the length of the string */
    for (l = 0; str[l] != '\0'; l++);

    /* Display the string backwards */
    for (i = l - 1; i >= 0; i--)
    {
        printf("%c", str[i]);
    }

    return 0;
}

Output

Enter a string: computer science
ecneics retupmoc