Reverse a string in C
×


Reverse a string in C

267

Reversing a string means changing the order of its characters, making the last character the first, the second last character the second, and so on.

This operation is often used in programming to manipulate strings or to check if a string is a palindrome(reads the same forwards and backwards).

Different Ways to Reverse a String:

Using strrev() Function:

The strrev() function is a library function that reverses a string in-place.

// Program for using strrev(() Function
#include<stdio.h>	
#include<string.h>		

int main() {
    char str[40];
    printf("\nEnter a string to be reversed: ");
    scanf("%s", str);

    int len = strlen(str);
    for(int i = 0; i < len/ 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[length - i - 1] = temp;
    }

    printf("\nAfter reversing the string: %s\n", str);
    return 0;
}		
	

Output:

Enter a string to be reversed: STRING
After reversing the string: GNIRTS

Without Using Library Function:

Here, we define our own function to reverse the string without using any library function.

// Program Without using Library Function
#include<stdio.h>	
#include<string.h>		

void reverseString(char *str) {
    int len = strlen(str);
    for(int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

int main() {
    char str[50];
    printf("\nEnter a string to be reversed: ");
    scanf("%s", str);
    reverseString(str);
    printf("\nAfter reversing the string: %s\n", str);
    return 0;
}		
	

Output:

Enter a string to be reversed: siya
After reversing the string: ayis

Using Recursion:

We can also use recursion to reverse a string by swapping the first and last characters and then recursively calling the function with the substring excluding the first and last characters.

// Program for using Recursion in C
#include<stdio.h>	
#include<string.h>		

void reverseStringRecursively(char *str, int start, int end) {
    if(start >= end) {
        return;
    }
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    reverseStringRecursively(str, start + 1, end - 1);
}

int main() {
    char str[50];
    printf("\nEnter a string to be reversed: ");
    scanf("%s", str);
    reverseStringRecursively(str, 0, strlen(str) - 1);
    printf("\nAfter reversing the string: %s\n", str);
    return 0;
}		
	

Output:

Enter a string to be reversed: reverse
After reversing the string: esrever

Key Points:

Using strrev() Function: Utilizes the built-in strrev()function from the C library to reverse the string in-place.

Without Library Function: Implements a custom function to reverse the string without using any library functions.

Using Recursion: Utilizes recursion to reverse the string by swapping characters recursively.

Each method demonstrates a different approach to reversing a string in C, providing flexibility and understanding of various techniques for string manipulation.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments