C Program to Reverse a String

In this tutorial, you will learn and get code for reversing a string in C. Reversing a string means

  • The first character of the string is transferred to the last, and the last character is transferred back to the first.
  • Similarly, the second character is assigned to the second last, and the second last character is assigned to the second.
  • and so on.

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

C Print the Reverse of a String

This program just prints 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;
}

This program was built and run in the Code::Blocks IDE. The following snapshot shows the output:

print string in reverse order c

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

c print string in reverse order

Consider another scenario in which the user enters any string containing spaces, such as "codes cracker dot com."

print string in reverse order c program

Program Explained

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

  • Receive any string as input from the user at run-time.
  • Calculate the length of the string using a for-loop that runs from the 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 that starts with the last character of the string and ends with the first character of the string.
  • Print the string character by character, starting with the last character, then the second last character, the third last character, and so on until you reach the first character.
  • In this way, you will get the string in reverse order as output.

C Reverse a String

Now this program actually reverses the given string first and then prints 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 the above program:

  • Accept any string
  • Because indexing begins at 0, initialize 0 to any variable, say i, that represents the first character of the string.
  • Get the length of the string using the strlen() function and initialize the 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 is less than j.
  • Now, inside any temporary variable, say temp, initialize the first character, then place the last character at the first character and the first character (the "temp" variable holds the first character) at the 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 each other.
  • Continue doing this until all the characters are reversed.
  • After coming out of the while loop, print the value of string, which will be the reverse of the original string.

The same program in different languages

C Quiz


« Previous Program Next Program »



Liked this post? Share it!