C – Reverse String

In natural language processing & parsing, sometimes it is easier to search a string from the end to the beginning. A string reverse would be useful for debugging, or as an alternate way to write the loop.String reversal function reverses the entered string i.e if the user enters a string “reverse” as input then it will be changed to “esrever”. strrev() function reverses a given string in C language under the header file string.h.

Note : A string which remains the same even after the reversal is called as palindrome string.

Syntax

The syntax of strrev() function is

char *strrev(char *string);
ADVERTISEMENT

Examples – Reverse String in C Programming

Following are the C programming examples we shall go through to reverse a string

  1. Reverse String using strrev() function
  2. Reverse String in place (swapping characters)
  3. Reverse String using recursion
  4. Reverse String using pointers

Example 1 – Reverse String using strrev()

In this example, we will use strrev() function to reverse a given string.

C Program

#include <stdio.h>
#include <string.h>

int main() {
	char str[100];
	printf("Enter a string \n");
	gets(str);
	strrev(str);
	printf("Reverse of the string is %s\n", str);
	return 0;
}

Example 2 – Reverse String In-place

Reverse a string in place means swapping characters at the beginning and the end of the string and then move towards the middle of the string. It doesn’t need a separate buffer to hold the reversed string.

In the following example, we will read a string from user and reverse this string in-place.

C Program

#include<stdio.h>
#include<string.h>
int main()
{
	char str[100],temp;
	int i=0,j=0;
	printf("enter string \n");
	gets(str);
	j=strlen(str)-1;
	while(i<j)
	{
		temp=str[j];
		str[j]=str[i];
		str[i]=temp;
		i++;
		j--;
	}
	printf("\n Reversed string is : ");
	puts(str);
	return 0;
}

Example 3 – Reverse String using Recursion

Recursive function takes string as an input and reverse the string until it encounters ‘\0’ by calling the reverse function. Though recursive function looks easier, it won’t give better performance than strrev() function because for every recursive call stack will be created.overhead of creating stack whenever a recursive function called will effects its efficiency.

In the following example, we will use this recursion technique to reverse a string read from the console.

C Program

#include<stdio.h>
char* reverse(char* str);                   // declaring recursive function
void main() {
	int i, j, k;
	char str[100];
	char *rev;
	printf("Enter the string:\t");
	gets(str);
	printf("The original string is:%s \t “,str);
	rev = reverse(str);
	printf("The reversed string is: ");
	puts(rev);
}
char* reverse(char *str) {                       // defining the function
	static int i = 0;
	static char rev[100];
	if(*str)
	{
	reverse(str+1);
	rev[i++] = *str;
	}
	return rev;
}

Output

Enter the string:       abcdefghijklmnopqrstuvwxyz
The original string is: abcdefghijklmnopqrstuvwxyz
The reversed string is: zyxwvutsrqponmlkjihgfedcba

Example 4 – Reverse String using Pointers

Another way of accessing a contiguous block of memory, instead of with an array, is with a pointer. Since we are talking about strings, which are made up of characters, we’ll be using pointers to characters i.e char *s.

However, pointers only hold an address, they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string, the character array containing the string must already exist. C program to reverse a string using pointers is given below.

In the following example, we will reverse the given string using String Pointer.

C Program

#include<stdio.h>
#include<string.h>

void reverse(char*);

int main() {
	char s[100];

	printf("Enter a string\n");
	gets(s);
	reverse(s);
	printf("Reverse of the string is \"%s\".\n", s)  
	return 0;
}

void reverse(char *s) {
	int length, c;
	char *begin, *end, temp;
	length = strlen(s);
	begin  = s;
	end    = s;

	for (c = 0; c < length - 1; c++)
		end++;

	for (c = 0; c < length/2; c++) {        
		temp   = *end;
		*end   = *begin;
		*begin = temp;

		begin++;
		end--;
	}
}

Conclusion

In this C TutorialReverse String in C Programming, we have learnt to use strrev() function to reverse a string. Also we have gone through other methods to reverse a string like, in-place reversing, reversing using pointers, reversing using recursion with C program examples.