How to reverse a string in C and C#?
The fastest way to reverse a string in C is an in-place two-pointer swap: one pointer starts at the first character, one at the last, and you swap until they meet. In C#, the simplest way is Array.Reverse() on a character array. Both run in O(n) time.
This answer shows the standard methods in both languages, with code you can use directly in a coding interview.
How to Reverse a String in C
1. Two-pointer swap (in place)
This is the classic C solution and the one interviewers expect. It uses O(1) extra space.
#include <stdio.h> #include <string.h> void reverseString(char str[]) { int left = 0; int right = strlen(str) - 1; while (left < right) { char temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main(void) { char text[] = "Hello, World!"; reverseString(text); printf("%s\n", text); // !dlroW ,olleH return 0; }
2. Loop into a new buffer
If you must keep the original string unchanged, copy characters back to front into a second array.
void reverseCopy(const char *src, char *dest) { int len = strlen(src); for (int i = 0; i < len; i++) { dest[i] = src[len - 1 - i]; } dest[len] = '\0'; }
3. Recursion
Recursion works but costs a stack frame per character, so use it only when asked.
void reverseRecursive(char str[], int left, int right) { if (left >= right) return; char temp = str[left]; str[left] = str[right]; str[right] = temp; reverseRecursive(str, left + 1, right - 1); }
A note on strrev(): it exists in some compilers (Turbo C, some MSVC versions) but it is not part of the C standard, so portable code and interview answers should use the two-pointer swap instead.
How to Reverse a String in C#
Strings in C# are immutable, so every method builds a new string.
1. Using Array.Reverse() (recommended)
public string ReverseString(string input) { char[] charArray = input.ToCharArray(); Array.Reverse(charArray); return new string(charArray); }
2. Using a for loop
public string ReverseString(string input) { string reversed = string.Empty; for (int i = input.Length - 1; i >= 0; i--) { reversed += input[i]; } return reversed; }
Avoid this for long strings: each += allocates a new string, making it O(n^2).
3. Using LINQ
using System.Linq; public string ReverseString(string input) { return new string(input.Reverse().ToArray()); }
4. Using StringBuilder
using System.Text; public string ReverseString(string input) { StringBuilder reversed = new StringBuilder(input.Length); for (int i = input.Length - 1; i >= 0; i--) { reversed.Append(input[i]); } return reversed.ToString(); }
Which method should you use?
| Method | Time | Extra space | Notes |
|---|---|---|---|
| C: two-pointer swap | O(n) | O(1) | In place, the interview answer |
| C#: Array.Reverse() | O(n) | O(n) | Cleanest C# option |
| C#: for loop with += | O(n^2) | O(n) | Avoid for long strings |
| C#: LINQ Reverse() | O(n) | O(n) | Concise, slight overhead |
| C#: StringBuilder | O(n) | O(n) | Best for building large strings |
Frequently Asked Questions
How do you reverse a string in Python or Java?
In Python it is one line: reversed_string = s[::-1]. In Java, use new StringBuilder(s).reverse().toString(). The underlying idea is the same two-pointer reversal.
Why does the interviewer care which method I pick?
String reversal is usually a warm-up to test whether you know about immutability, in-place operations, and time/space complexity. Mentioning that the two-pointer swap is O(n) time and O(1) space is what earns the follow-up question.
Does this handle Unicode correctly?
Byte-by-byte or char-by-char reversal breaks multi-byte characters (emoji, combining accents). For interview problems ASCII is assumed; in production, reverse by grapheme clusters using your platform's text APIs.
String reversal is one of the most common warm-up questions in coding interviews. If you are preparing seriously, the two-pointer technique behind it is one of the core patterns in Grokking the Coding Interview, and Grokking Data Structures covers the array and string fundamentals these questions build on.
Related Questions

GET YOUR FREE
Coding Questions Catalog

$123

$197

$72