The Wayback Machine - https://web.archive.org/web/20240723230028/https://www.geeksforgeeks.org/reverse-string-in-c/
Open In App

Reverse String in C

Last Updated : 05 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Reversing a string in C is a fundamental operation that involves rearranging the characters in a string so that the last character becomes the first, the second-to-last character becomes the second, and so on.

For example,

Original String:
"string"

Reversed String:
"gnirts"

In this article, we will discuss different ways to reverse a string in C with code examples.

Different Ways to Reverse a String in C

There are various ways to reverse the string in the C. Some of them are discussed below:

  1. Reverse the String Using Loop
  2. Reverse the String Using Recursion
  3. Reverse the String Using Pointer in C
  4. Reverse the String Using Library Function

1. Reverse the String Using Loop

In this method,

  • We use a for loop with two variables i and j pointing to the start and end of the string respectively.
  • The we replace the characters at indexes i and j, and move to the adjacent right and left respectively i.e. incrementing i and decrementing j.
  • We keep doing that till i is greater than or equal to j.

We get the reversed string as the result.

Implementation

C




// C program to reverse the string in C using loops
#include <stdio.h>
#include <string.h>
 
int main()
{
    // string to be reversed.
    char str[100] = "string";
 
    printf("Original String: %s\n", str);
 
    // string length
    int len = strlen(str);
 
    // for loop
    for (int i = 0, j = len - 1; i <= j; i++, j--) {
        // swapping characters
        char c = str[i];
        str[i] = str[j];
        str[j] = c;
    }
 
    printf("Reversed String: %s", str);
 
    return 0;
}


Output

Original String: string
Reversed String: gnirts

2. Reverse the String Using Recursion

For this method, we will use recursion to swap the characters.

Implementation

C




// C program to reverse string using recursion
#include <stdio.h>
#include <string.h>
 
// recursive function to reverse string
void reverse(char* str, int len, int i, int temp)
{
    // if current index is less than the remaining length of
    // string
    if (i < len) {
        temp = str[i];
        str[i] = str[len - 1];
        str[len - 1] = temp;
        i++;
        len--;
        reverse(str, len, i, temp);
    }
}
 
// driver code
int main()
{
    char str[100] = "string";
    printf("Original String: %s\n", str);
 
    int len = strlen(str);
    reverse(str, len, 0, 0);
 
    printf("Reversed String: %s", str);
    return 0;
}


Output

Original String: string
Reversed String: gnirts

3. Reverse the String Using Pointer in C

We will use here two pointers, one is start pointer and another is end pointer. and by swapping the character we will proceed to achieve, reverse the characters similar to what we have done in the first method.

Implementation

C




// C program to reverse a string using pointers
#include <stdio.h>
#include <string.h>
 
// function to reverse the string
void stringReverse(char* str)
{
    int len = strlen(str);
    // pointers to start and end
    char* start = str;
    char* end = str + len - 1;
 
    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}
 
// driver code
int main()
{
    char str[] = "string";
    printf("Original String: %s\n", str);
 
    // calling function
    stringReverse(str);
 
    printf("Reversed String: %s", str);
    return 0;
}


Output

Original String: string
Reversed String: gnirts

4. Reverse the String Using Library Function

In C, we have a library function defined inside <string.h> that can be used to reverse a string. The strrev() function provides the simplest method to reverse the string.

Syntax

char* strrev(char* str);

where, str is the string to be reversed.

Note: The strrev() function is not a part of the standard C language, so it might not be present in every compiler.

Implementation

C




// C program to reverse a string using strrev()
#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[] = "string";
    printf("Original String: %s\n", str);
 
    // reversing string
    printf("Reversed String: %s", strrev(str));
 
    return 0;
}


Output

Original String: string
Reversed String: gnirts



Similar Reads

Reverse a string in C/C++ using Client Server model
This article describes a Client and Server setup where a Client connects, sends a string to server and the server shows the original string and sends reversed string to client using socket connection. Prerequisite : Socket Programming Examples: Input : welcome Output :emoclew Input :geeks for geeks Output :skeeg rof skeeg Explanation In this, first
3 min read
Program to Reverse a String using Pointers
Given a string, the task is to reverse this String using pointers. Examples: Input: Geeks Output: skeeG Input: GeeksForGeeks Output: skeeGroFskeeG Approach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then reversed one by one with the help of these t
2 min read
C Program to Reverse a String Using Recursion
Reversing a string means changing the order of characters in the string so that the last character becomes the first character of the string. Example: Input: Geeks Output: skeeG Input: Hello Output: olleHSimple Approach Below is the C program to reverse a string using recursion: C/C++ Code // C program to reverse a string // using recursion # inclu
3 min read
C Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "practice of lot a needs coding at good getting" Rec
3 min read
C# Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "practice of lot a needs coding at good getting" Rec
4 min read
How to Reverse a String in C?
In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Input: char myStr[] = "Hello"; Output: Reversed String
2 min read
C Program for Reverse a linked list
Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input : Head of following linked list 1-&gt;2-&gt;3-&gt;4-&gt;NULL Output : Linked list should be changed to, 4-&gt;3-&gt;2-&gt;1-&gt;NULL Input : Head of following linked list 1-&gt;2-&gt;3
4 min read
Substring Reverse Pattern
Given string str, the task is to print the pattern given in the examples below: Examples: Input: str = "geeks" Output: geeks *kee* **e** The reverse of "geeks" is "skeeg" Replace the first and last characters with '*' i.e. *kee* Replace the second and second last character in the modified string i.e. **e** And so on. Input: str = "first" Output: fi
5 min read
Reverse an array in groups of given size | Set 3 (Single traversal)
Given an array, reverse every sub-array formed by consecutive k elements. Examples: Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3. Output: [3, 2, 1, 6, 5, 4, 9, 8, 7, 10]Input: arr = [1, 2, 3, 4, 5, 6, 7], k = 5. Output: [5, 4, 3, 2, 1, 7, 6] Approach: We will use two pointers technique to solve this problem.First, we will initialize our 1st
6 min read
C program to reverse the content of the file and print it
Given a text file in a directory, the task is to print the file content backward i.e., the last line should get printed first, 2nd last line should be printed second, and so on.Examples: Input: file1.txt has: Welcome to GeeksforGeeks Output: GeeksforGeeks to WelcomeGeeksforGeeks Input: file1.txt has: This is line one This is line two This is line t
3 min read
Article Tags :
three90RightbarBannerImg