Index « Previous Next »

Question

Write a program that prompts the user to input a string andĀ convert the string in reverse.

Source Code

#include <stdio.h>

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

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

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

    /* reverse the string */
    for (i = 0, j = l - 1; i < l / 2; i++, j--)
    {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }

    printf("Reverse string: %s", str);

    return 0;
}

Output

Enter string: computer science
Reverse string: ecneics retupmoc