How to reverse a string in C#?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Reversing a string in C# can be accomplished in several ways, depending on the specific requirements or constraints of your application. Here are a few common methods to reverse a string in C#:

1. Using a 'for' loop

You can manually reverse a string by iterating through it from the last character to the first and appending each character to a new string.

public string ReverseString(string input) { string reversed = string.Empty; for (int i = input.Length - 1; i >= 0; i--) { reversed += input[i]; } return reversed; }

2. Using 'Array.Reverse()'

Another approach is to convert the string into a character array, use the Array.Reverse() method to reverse the array, and then create a new string from the reversed array.

public string ReverseString(string input) { char[] charArray = input.ToCharArray(); Array.Reverse(charArray); return new string(charArray); }

3. Using LINQ

If you prefer a more concise syntax, you can use LINQ to reverse the string. This method involves converting the string to a character array, reversing it, and then converting it back to a string.

using System.Linq; public string ReverseString(string input) { return new string(input.ToCharArray().Reverse().ToArray()); }

4. Using 'StringBuilder'

For better performance, especially with very long strings, using a StringBuilder can be more efficient than concatenating strings with a for loop.

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(); }

Example Usage

Here is how you could use one of these methods in a simple C# program:

using System; class Program { static void Main() { string originalString = "Hello, World!"; string reversedString = ReverseString(originalString); Console.WriteLine($"Original: {originalString}"); Console.WriteLine($"Reversed: {reversedString}"); } static string ReverseString(string input) { char[] charArray = input.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } }

Choosing the Right Method

  • Simple Loops are straightforward but may not be the most efficient for large strings due to string immutability in C#.
  • Array.Reverse() is a neat and effective way to reverse a string and works well for most cases.
  • LINQ offers a concise approach but can be slightly slower due to additional overhead.
  • StringBuilder is typically the best choice for performance-sensitive contexts or when dealing with very large strings.

Each method has its advantages, and the best choice depends on your specific requirements, such as performance considerations and code readability preferences.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

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
Can you get a coding job after bootcamp?
Does Palantir allow remote work?
Why should we hire you as a cloud engineer?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.