Reader Level:
ARTICLE

Reverse a String Without Using Function in C#

Posted by Vithal Wadje Articles | C# Language November 02, 2012
In this article we will learn how to reverse a string without using any string functions.
  • 1
  • 0
  • 39371

Background


I will explain in this article how to reverse a given string in a Windows console application using C# without using a function. This type of question might be asked by an interviewer in a .Net position related interview.

My intent for this article is to explain how to answer a question that is often asked in an interview, which is: Write a program to reverse the given String without using the string function. A candidate new to the interview can become totally confused because the first problem is the candidate just thinks about functions, not other techniques to reverse a string.

So considering the preceding requirement and numerous emails sent to me from beginners I have decided to write this article with the basics and specially focusing on the beginner, student and whoever might face this type of question in an interview.

So let us start with the basics.

What is a String ?

A String is a sequence of charcters enclosed within double quatation marks.

Example

"vithal wadje" ,   "C#" , "Mumbai"  "Five hundred"  etc.

I hope you understand about strings now.

What reversing String Means ?

To change the postion of charters from right to left one by one is called reversing a string.

Example
Suppose I have the given input as:

vithal wadje

Then the given string can be reversed into:

ejdaw lahtiv

Now that we completely understand what is meant by reversing a string, next I will explain how to do it step-by-step, as in the following:
  1. Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
  2. Then go to to "File" -> "New" -> "Project..." then select Visual C# -> Windows -> Console application.
  3. After that specify the name such as ReverseString or whatever name you wish and the location of the project and click on the OK button. The new project is created.
And use the following code in the program.cs class file:

using System;

namespace ReverseString
{
    class Program
    {
        static void Main(string[] args)
        {

    string Str, Revstr = "";  //for storing string value
    int Length;               //for counting lenght of given string

    Console.Write("Enter A String : "); //showing message to user
    Str = Console.ReadLine();           //to allow user to input string

    Length = Str.Length - 1;            //storing the length of given string

    while (Length >= 0)                //loops the given string length
    {

        Revstr = Revstr + Str[Length];  //performimg a reverse string according to length of given string
        Length--;

    }

    Console.WriteLine("Reverse  String  Is  {0}", Revstr); // displaying output to user
    Console.ReadLine();                                                // to keep window
}
    }
}


In the above program I have clearly explained in comments which statement is used for which purpose. Now run the program with the input string vithal wadje; the output will be as follows:

revrsestring.png

Summary

From the all examples above I have explained how to reverse the string. I hope this article is useful for begainers and anyone prepairing for an interview. If you have any suggestions and feedback please contact me.

COMMENT USING

Trending up