Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

String Reversal with loops and without loops

Hi all,
This is a simple coding snippet for Reversing a user inputted string using C#, tried out with various ways and techniques.  Here we go…
Topics covered: (1) String Reversal with loops and without loops , (2) Char[chrArray] to string conversion
               //█ String reversal – with loop
        private void button1_Click(object sender,EventArgs e)
        {
            string _strInput = "ABCDE";
            char[] _strResult = new char[_strInput.Length];
            int _count = 0;
            for(int i = _strInput.Length – 1;i > -1;i–)
            {
                _strResult[_count] = Convert.ToChar(_strInput[i]);
                _count++;
            }
            // tip #1 // Converting Char[] to String – using stringbuilder class
            StringBuilder strm = new StringBuilder();
            strm.Append(_strResult,0,_strInput.Length);
            MessageBox.Show(strm.ToString());
 
            // tip #2 // Converting Char[] to String – using string constructor
            string _strPrint = new string(_strResult);
            MessageBox.Show(_strPrint);
        }
          //█ String reversal – without loop     
        private void button2_Click(object sender,EventArgs e)
        {
            MessageBox.Show(Reverse("ABCDE"));
        }
        //Recursive function (author : unknown,from net )
        string Reverse(string _strParam)
        {
            if(_strParam.Length == 1)
                return _strParam;
            else
               return Reverse(_strParam.Substring(1)) + _strParam.Substring(0,1);
        }
  Is there any other way of reversing a string..? Hope someone try out if any other possibility exist ..I mean without looping and recursive techniques…..
Mahesh~
Advertisements

November 6, 2007 - Posted by | .NET General

2 Comments »

  1.            My way of string reversal 😀 string st="ajai was here !";            Array ar = st.ToCharArray();            Array.Reverse(ar);            st=new string((char[])ar);            Console.WriteLine(st);            //http://ajaispam.blogspot.com/

    Like

    Comment by Ajai | November 6, 2007 | Reply

  2. Hmm. Array.Reverse is also much appreciated way..let me try..thanks ajay.

    Like

    Comment by Mahesh | November 7, 2007 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: