C program to reverse string

Written by

Pooja Rao

Reverse a string in C

Another string operation that we are going to look at is reversing a given string.

Reverse of a string “HELLO ” would be “OLLEH”,  and in order to do so, we would have to swap the first and last characters, the second and the second last characters and so on.

First, let us see the simpler approach of using pre-defined C functions.

Approach 1: Using the functions strrev() from string.h:

  • strrev() reverses a given input string.
  • This is again a not recommended approach as strlwr and strupr are from Microsoft C library and does not work in standard C library. If you are using a Turbo C++ IDE, only then will the below approach work. The GNU library has no such function defined and will throw you an error.
  • Here to scan the string we are using fgets method in order to understand how it is used, although it is not recommended.

Code:

#include <stdio.h>
#include<conio.h>
#include <string.h>    //include the header file which contains definition of strrev function

int main(){ clrscr(); char string[100] ; printf("Enter a string : \n"); fgets(string, 100, stdin);     //read string input from standard input screen. printf("Reverse is :\n", strrev(string)); getch(); return  0; }


Output:

Approach 2:  Without using pre-defined functions of C:

  • In this approach, we will use 3 ways to reverse the string.
  • Number 1: We will just display the string in reverse. We shall just display the string characters using the index in reverse order.
  • Number 2: We will store the characters of the original string in reverse order in a string called ‘reverse_string’.
  • Number 3: We will use a temporary character called ‘temp’, to store the ith character which will initially be the first character on a temporary basis; while we store the last character jth character , at the first position (ith) in the string. The temp value is then assigned to the jth place.

Code:

#include <stdio.h>

int main()

{

int size = 100, bytes_read, length, i, j;

char *string, reverse_string[100], temp;

printf("Enter some text of your choice: ");

string = (char *) malloc (size);

bytes_read = getline (&string, &size, stdin);

length = bytes_read - 1;

//Just displaying string in reverse

printf("Displaying the string in reverse: ");

for(i= (length - 1); i>=0; i--)

{

putchar(string[i]);

}

j = 0;   //initalizing counter for original string

//Reverse string stored in another string

for(i= (length - 1) ; i >= 0; i--)

{

reverse_string[i] = string[j];  //storing the value of string in reverse order

j++;

}

reverse_string[length] = '\0';     //string termination

printf("\n\nThe reverse of string is: %s\n", reverse_string);

//Using a temp character and making changes in the original array

i = 0;

j = length - 1;

while (i < j)

{

temp = string[i];

string[i] = string[j];

string[j] = temp;

i++;

j--;

}

printf("\n\nThe reverse string is: %s\n", string);

return 0;

}

Output:

Enter some text of your choice: hello World !

Displaying the string in reverse: ! dlroW olleh

The reverse of string is: ! dlroW olleh

The reverse string is: ! dlroW olleh

Approach 3: Using Recursion:

  • In the recursive method, we swap characters at the beginning and the end of the string and then move towards the middle of the string.
  • This way is inefficient due to repeated function calls.
  • Our Base case is when we reach the mid of the array; that means no more swaps are pending.

Code:

#include <stdio.h>

void reverse(char str[], int index, int size)

{

char temp;

temp = str[index];

str[index] = str[size - index];

str[size - index] = temp;

if (index == size / 2)

{

return;

}

reverse(str, index + 1, size);

}

int main()

{

int size = 100, bytes_read, length;

char *string;

printf("Enter some text of your choice: ");

string = (char *) malloc (size);

bytes_read = getline (&string, &size, stdin);

length = bytes_read - 1;

reverse(string, 0, (length - 1));

printf("The string after reversing is: %s\n", string);

return 0;

}

Output:

Enter some text of your choice: Hello! Morning !

The string after reversing is: ! gninroM !olleH

 

Approach 4: Using Pointers:

  • The logic is same as used above; however we do the same with pointers and without recursion.
  • What we do is, we have a pointer ‘start’ point to the start of the array and ‘end’point to the end of the array.
  • We traverse till the mid of the string and keep swapping the start and end characters using temp character variable.

Code:

#include <stdio.h>

void reverse(char *str,int length)

{

int i;

char *start, *end, temp;

start  = str;     //pointer to beginning of string

end    = str;     //pointer to end of string

for (i = 0; i < length - 1; i++)

end++;

for (i = 0; i < length/2; i++)

{

temp   = *end;

*end   = *start;

*start = temp;

start++;

end--;

}

}

int main()

{

int size = 100, bytes_read, length;

char *string;

printf("Enter some text of your choice: ");

string = (char *) malloc (size);

bytes_read = getline (&string, &size, stdin);

length = bytes_read - 1;

reverse(string, (length - 1));

printf("The string after reversing is: %s\n", string);

return 0;

}

Output:

Enter some text of your choice: Found You !

The string after reversing is:  uoY dnuoF!

 

C program to reverse string