0
0
CHow-ToBeginner · 3 min read

How to Reverse a String in C: Simple Syntax and Example

To reverse a string in C, you swap characters from the start and end moving towards the center using a loop. This is done by iterating with two indexes and exchanging characters until they meet or cross.
📐

Syntax

To reverse a string in C, you typically use a for or while loop with two indexes: one starting at the beginning and one at the end of the string. You swap the characters at these positions and move the indexes towards the center until they meet.

  • char str[]: the string to reverse
  • int start: index starting at 0 (beginning)
  • int end: index starting at strlen(str) - 1 (end)
  • Swap characters at start and end
  • Increment start, decrement end until start >= end
c
#include <string.h>

void reverseString(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;
    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}
💻

Example

This example shows a complete program that reverses a string entered by the user and prints the reversed string.

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

void reverseString(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;
    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    // Remove newline character if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}
Output
Enter a string: hello Reversed string: olleh
⚠️

Common Pitfalls

Common mistakes when reversing strings in C include:

  • Not handling the string's null terminator properly.
  • Forgetting to remove the newline character when using fgets.
  • Using char * pointers without allocating writable memory (strings from string literals are read-only).
  • Swapping characters incorrectly or using wrong loop conditions.
c
/* Wrong: Using string literal (read-only) */
// char *str = "hello";
// reverseString(str); // May cause crash

/* Right: Use writable array */
char str[] = "hello";
reverseString(str);
📊

Quick Reference

  • Use two indexes: start at 0, end at strlen(str) - 1.
  • Swap characters at start and end.
  • Move start forward and end backward until they meet.
  • Ensure string is writable (use char array, not string literal).
  • Remove newline if using fgets before reversing.

Key Takeaways

Reverse a string by swapping characters from start and end moving inward.
Always use a writable char array, not a string literal, to avoid crashes.
Remove newline characters when reading input with fgets before reversing.
Use a loop with two indexes until they meet or cross to reverse correctly.
Check string length with strlen to set the end index properly.