C program to print the string in reverse order

Printing the string in reverse order.

In this article we will learn how to code a C program to print the string in reverse order. C programming provides  ” strrev() ” a predefined function to reverse any given string, but to understand how this function actually works we will be reversing the string manually using for loop.

C program to print the given string in reverse order.

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Find the length.
  • Initialize for loop.
  • Reverse the string by iterating each character.
  • Terminate for loop.
  • Print result.

C programming code to print the given string in reverse order 

Competitive Coding Techniques
#include <stdio.h>
#include <string.h>
int main()
{
    //Initializing variable.
    char str[100];
    char rev[100];
    int i, j, len=0;
    
    //Accepting input.
    printf(" Enter a string: ");
    gets(str);
    //Calculating length.
    len = strlen(str);
    
    //Reversing and printing the string using for loop. 
    printf("The reverse string is: ");
    for(i = len - 1; i >= 0; i--) 
    {
         printf("%c", str[i]);
    }
    
    return 0;
}
Output:
Enter a string: prepinsta
The reverse string is: atsniperp