In this C# tutorial explains, how to reverse a string in c# using a while loop with complete code with examples. I will show you, how to reverse a string in C# without using for loop.
Reverse a string in C# using a while loop
The logic behind this is like below:
- Start at the end of the string.
- Fetch one character at a time.
- Build a new string by appending each character.
Here is a complete code of how to reverse a string in C# using a while loop.
using System;
class Program
{
public static string ReverseString(string str)
{
int index = str.Length - 1; // Start at the last character
string reversed = string.Empty; // Initialize an empty string for reversed version
while (index >= 0)
{
reversed += str[index]; // Append the character to the reversed string
index--; // Move to the previous character
}
return reversed;
}
static void Main()
{
Console.WriteLine("Enter a string:");
string input = Console.ReadLine();
string reversed = ReverseString(input);
Console.WriteLine($"Reversed string: {reversed}");
}
}
You can see, I enter the input as “United States” and it provides the reverse string.

Here is the code explanation:
- The
ReverseString
method takes a string as an input. - The
index
variable is initialized to the last position of the string. - The
reversed
string starts as an empty string. - The while loop continues as long as the
index
is non-negative (i.e., as long as we haven’t reached the beginning of the string). - Inside the loop, we append the character at the current
index
position of the original string to thereversed
string. Then, we decrement theindex
to move to the previous character. - Finally, the reversed string is returned.
Conclusion
In this C# tutorial, I have explained with an example, how to reverse a string in C# using a while loop.
You may also like:
- How to Check if an Array is Empty in C#?
- Dynamic Arrays in C# with Examples
- Reverse a String in C# Using a For Loop
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…