C Program to Reverse a String

In this tutorial, you will learn and get code about reversing a string in C. Reverse of a string means

  • First character of string goes to last, and last character come back to first
  • Similarly second character goes to second last, and second last character goes to second
  • and so on

For example, if the given string is codescracker, then its reverse will be rekcarcsedoc

C Print Reverse of a String

This program just print the given string in reverse order without actually reversing it:

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[50], i, j, count=0;
    printf("Enter any string: ");
    gets(str);
    for(i=0; str[i]!='\0'; i++)
        count++;
    for(j=(count-1); j>=0; j--)
        printf("%c", str[j]);
    getch();
    return 0;
}

The program was written under Code::Blocks IDE, therefore after successful build and run. You will get the following output:

print string in reverse order c

Provide any string say codescracker and press ENTER key to see the string in reverse order:

c print string in reverse order

Let's take another sample run where user has provided any string that contains spaces say codes cracker dot com:

print string in reverse order c program

Program Explained

Here are the list of some main steps used in above program:

  • Receive any string as input from user at run-time
  • Calculate the length of the string using for loop, that runs from first character of the string to the last character of the string. The last character present before the null terminated character ('\0')
  • Now create a for loop starts with last character of the string to the first character of the string
  • Print the string, character by character, that is print last character, second last character, third last character, ..., first character
  • In this way, you will get the string in reverse order as output

C Reverse a String

Now this program actually reverse the given string first, and then will print it as output:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], temp;
    int i=0, j;
    printf("Enter the String: ");
    gets(str);
    i=0;
    j=strlen(str)-1;
    while(i<j)
    {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++;
        j--;
    }
    printf("\nReverse of the String is:\n%s", str);
    getch();
    return 0;
}

Here is the first sample run:

c program to reverse string

Here is another sample run:

c reverse any string

Program Explained

Here are some of the main steps used in above program:

  • Receive any string
  • As the indexing starts from 0, therefore initialize 0 to any variable say i, that represents the first character of the string initially
  • Get the length of the string using strlen() function and initialize length of the string minus one to any variable say j, that represents the last character of the string initially
  • Now create a while loop, that runs until i less than j
  • Now inside any temporary variable say temp, initialize the first character, then place last character at first character and first character (temp variable holds first character) at last character, and increment the value of i and decrement the value of j
  • Now i holds the second character and j holds the second last character
  • Replace with each other or do the reverse operation as done before
  • Continue doing this until all the characters gets reversed
  • After coming out from the while loop, print the value of string which will be the reverse of the original string

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋