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.

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?

MethodTimeExtra spaceNotes
C: two-pointer swapO(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#: StringBuilderO(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.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What language is used in AWS code?
What is the fastest way to learn software engineering?
Is Apple interview harder than Google?
What Splunk is used for?
What skills do Apple employees need?
What is Netflix main objective?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.