OK, conceptually, it’s quite easy. A couple of my friends got asked this question when interviewing. Here are what my solutions are:
1. The trivial way
This is the probably the most trivial version:
public string Reverse1(string s)
{
char[] c = s.ToCharArray();
string ts = string.Empty;for (int i = c.Length – 1; i >= 0; i–)
ts += c[i].ToString();return ts;
}
2. Using a stack
This method is essentially the same as the first one, however it uses the FILO nature of the stack to accomplish the same task.
public string Reverse(string s)
{
Stack<string> stack = new Stack<string>();for (int i = 0; i < s.Length; i++)
stack.Push(s.Substring(i, 1));string ts = string.Empty;
for (int i = 0; i < s.Length; i++)
ts += stack.Pop();return ts;
}
3. Using recursion (The cool way)
This version is probably the coolest and might just be what your interviewer was looking for. It actually does not need extra storage (it uses the stack but you do not need to programmatically allocate space in your code like the previous two cases).
Here’s the how you would use the Reverse method:
public string Reverse(string s, int l)
{
if (l == 1)
return s;
else
return Reverse(s.Substring(1, s.Length – 1), –l) + s[0].ToString();
}
Here’s the how you would use the Reverse method:
string s = “ABCDEFGHIJK”;
Console.WriteLine(Reverse(s, s.Length));
Hello. I need to reverse a string using stack. I read this and truely i cant understand because i m a beginner .. I know push and pop codes. Will you please elaborate reverse function.
Hi Kerry,
I’ve tried your 3rd approach of reversing a string. It doesn’t seem correct when I copy/paste your provided code snippet.
Thanks in advance
this code for recursion works
using system;
{
class reversedemo
{
string output=string.empty;
public void reverse(string s)
{
if(input.length>0)
{
output+=s.substring(s.length-1);
s=s.substring(0,s.length-1);
reverse(s);
}
}
console.writeline(output);
public static void main()
{
reversedemo rd=new reversedemo();
rd.reverse(“abcd”);
}
}
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
Hi Kerry D,
I tried all the methods but third one is not working. But it looks good. could you please recode it.
Pavan
private string recursive(string s, int i)
{
if (i == 0)
{
return s.Substring(s.Length/2,s.Length/2);
}
else
{
return recursive(s + s.Substring(i-1,1), i-1);
}
}
following code also works for the recursive method:
/************************************************/
public string Reverse(string s, int l)
{
if (l == 1)
return s;
else
return Reverse(s.Substring(l – (s.Length – 1), s.Length – 1), l – 1) + s[0].ToString();
}
/************************************************/
usage is the same as above
Here is another version, it has many variations
/************************************************/
public string Reverse(string s, int l)
{
if (l == 1)
return s;
else
return s[l-1].ToString()+ Reverse(0,l-1), l – 1) +
}
/************************************************/
protected void btnsubmit_Click(object sender, EventArgs e)
{
string str_in = txtinput.Text.Trim();
string output = “”;
for (int i = str_in.Length-1; i >= 0; i–)
{
output = output + str_in.Substring(i,1);
}
txtoutput.Text = output;
//another way
char[] cstr = str_in.ToCharArray();
Array.Reverse(cstr);
txtoutput.Text = reversestring(cstr);
}
public string reversestring(char[] newq)
{
return new string(newq);
}
this works perfectly..kerry example gives index out of range error..may be its his typo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
public static string ReverseString1(String str, int len)
{
if (len == 1)
return str;
else
return ReverseString1(str.Substring(1, str.Length – 1), len-1)+str[0].ToString();
}
static void Main(string[] args)
{
string test = “This is a test”;
int length = test.Length;
string result=ReverseString1(test, length);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
Console.WriteLine(“Enter a string :”);
string Name = Console.ReadLine();
char[] character = Name.ToCharArray();
Console.WriteLine(“Entered String is: ” + Name);
Array.Reverse(character);
Console.WriteLine(“After reverse string is :”);
Console.WriteLine(character);
Console.ReadKey();
}