Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing a String

Tags:

c#

logic

reverse

I was just researching how can I reverse a String. I found a Program in C# The Complete Reference Book but i was not able to understand it. Plz anyone Explain me how it works

The Program is here:

using System;
class RevStr 
{
    // Display a string backward.
    public void DisplayRev(string str) 
    {
        if(str.Length > 0)
            DisplayRev(str.Substring(1, str.Length-1));
        else
            return;

        Console.Write(str[0]);
    }
}

class RevStrDemo 
{
    static void Main() 
    {
        string s = "this is a test";
        RevStr rsOb = new RevStr();
        Console.WriteLine("Original string: " + s);
        Console.Write("Reversed string: ");
        rsOb.DisplayRev(s);
        Console.WriteLine();
    }
}
  1. How does this program works?
  2. Does the statement rsOb.DisplayRev(s); call DisplayRev(str.Substring(1, str.Length-1)); for every Character in the string?
like image 925
Sarao Avatar asked Jul 20 '26 21:07

Sarao


2 Answers

What I'd Recommend

Have you tried myString.Reverse();

It would be the easiest way to reverse a string in C# (.Net 3.5 and above).

Response

As for your example it is using Recursion to display each character by removing the end character (by creating a substring of the original with the first char removed) then calling itself again until there are no characters remaining.

You will only need to call DisplayRev(s); once because it recursively displays each character within.

Explanation Attempt

For example we have the string "hello".

The first call will recursively call the function with the substring of "hello" which will be "ello"

Note that the call to the Console.WriteLine hasn't been made, because the function DisplayRev has been called so we then step in to the recursive call.

This will keep on occuring until the string is empty (.Length == 0). Then the function will exit and return to the caller, which will be where the string[0] is "o", the Console.WriteLine code will be hit writing the "o" to the console, this function will then be exited and the caller of that will then hit the Console.WriteLine, which will be the "l", this will keep returning until the initial call is reached.

Thus resulting in your initial string, reversed.

Reference

There are a number of tutorials around about recursion. Here's a few (they'll be better at explaining it than I am):

Recursion Example 1
Recursion Example 2

like image 130
Lloyd Powell Avatar answered Jul 22 '26 12:07

Lloyd Powell


No.

It uses Recursive calls

DisplayRev calls itself with one less char

when it get to the end , and back from the recursion - it prints the char.

Also notice that its just display the reveresed string and not actually revere it

like image 31
Royi Namir Avatar answered Jul 22 '26 12:07

Royi Namir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!