Reversing Strings in C Programming

Reversing a string is a common operation in programming, and C is no exception. In this tutorial, we'll explore how to reverse a string in C programming using different methods.

Method 1: Using a Temporary Array

One way to reverse a string in C is to use a temporary array to store the characters of the original string in reverse order.

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

void reverse_string(char *str) {
    int length = strlen(str);
    char temp[length + 1];
    int i;

    for (i = 0; i < length; i++) {
        temp[i] = str[length - i - 1];
    }
    temp[length] = '\0';

    strcpy(str, temp);
}

int main() {
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    reverse_string(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

Method 2: Using Two Pointers

Another way to reverse a string in C is to use two pointers, one starting from the beginning of the string and one from the end.

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

void reverse_string(char *str) {
    int length = strlen(str);
    char *start = str;
    char *end = str + length - 1;
    char temp;

    while (start < end) {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main() {
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    reverse_string(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

Method 3: Using Recursion

We can also reverse a string in C using recursion.

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

void reverse_string(char *str) {
    if (*str == '\0') return;
    reverse_string(str + 1);
    printf("%c", *str);
}

int main() {
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    printf("Reversed string: ");
    reverse_string(str);
    printf("\n");
    return 0;
}

Conclusion

Reversing a string in C programming is a simple operation that can be achieved using different methods. In this tutorial, we've explored three methods: using a temporary array, using two pointers, and using recursion. Each method has its own advantages and disadvantages, and the choice of method depends on the specific requirements of the problem.

ShareTwitterShareFacebookShareLinkedin

🌻 Latest Blog Posts: Stay Informed and Inspired

Explore the latest and greatest from our blog! Dive into a diverse range of topics.

Mastering PHP: Sending GET Requests with Ease

explore the power of PHP in making HTTP GET requests, using the popular JSONPlaceholder API as an example. You'll learn how to send GET requests, handle the response, and extract valuable data.

Understanding Pointers in C Programming with Examples

Explore the fundamentals of pointers in C programming with detailed examples and explanations. Learn how to use pointers effectively in your C code.

Reversing Arrays in C: A Step-by-Step Guide

Learn how to reverse arrays in C programming with examples and explanations. Understand the different methods to reverse an array, including using a temporary array, swapping elements, and using recursion.

Reversing Strings in C Programming: A Step-by-Step Guide

Learn how to reverse strings in C programming with examples and code snippets. Discover three methods: temporary array, two pointers, and recursion.