How to reverse a string without using array and for loop
Loading
How to reverse a string without using array and for loop
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Brahma Prakash ShuklaPosted Jun 29, 2023, 1:45 PM
Janarthanan SPosted Jun 29, 2023, 9:53 AM
using System;
using System.Text;
class Program
{
static void Main()
{
string input = "Hello, World!";
string reversed = ReverseString(input);
Console.WriteLine(reversed);
}
static string ReverseString(string input)
{
if (input.Length <= 1)
return input;
StringBuilder sb = new StringBuilder();
sb.Append(input[input.Length - 1]);
sb.Append(ReverseString(input.Substring(0, input.Length - 1)));
return sb.ToString();
}
}
Amit MohantyPosted Jun 29, 2023, 9:32 AM
cjardPosted Jun 29, 2023, 6:11 AM
using LINQ:
string.Concat(yourStringVariableHere.Reverse());
note that LINQ uses loops internally. You can't easily reverse a string without using loops at all