# VBForums CodeBank > CodeBank - C# >  strReverse in C#

## dynamic_sysop

no need for a demo project , just copy the function and paste to your application to use.
this carries out the same function as strReverse does in VB.

VB Code:
[COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
        {
            MessageBox.Show(StringReverse("test 123"));
        }
         [COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
        {
            Array arr = ToReverse.ToCharArray();
            Array.Reverse( arr ); [COLOR=Green]// reverse the string[/COLOR]
            [COLOR=Blue]char[/COLOR][] c = ([COLOR=Blue]char[/COLOR][])arr;
            [COLOR=Blue]byte[/COLOR][] b = System.Text.Encoding.Default.GetBytes(c);
            [COLOR=Blue]return[/COLOR] System.Text.Encoding.Default.GetString(b);
        }

----------


## brown monkey

i don't know much mate but i've got this


```

      public string strReverse(string s)
      {
         int j=0;
         char[] c=new char[s.Length];
         for(int i=s.Length-1;i>=0;i--) c[j++]=s[i];
         return new string(c);
      } 



```

hehe. not efficient enough compare to that of your code mate. and oh. sorry for bothers...

----------


## hellswraith

I know it is frowned apon, but if you need to make use of it, why not just reference the Microsoft Visual Basic Runtime, then you can use this code:

MessageBox.Show(Microsoft.VisualBasic.Strings.StrReverse(textBox1.Text));

----------


## ricka0

The classic interview question is "How do you reverse all the strings but preserve their order, ie, first is last and last is first"?
In the example, "test 123" become "123 test" or
"test 123 ricka" become "ricka 123 test" 

VB Code:
string reverseEachString(string s){
             System.Text.StringBuilder  sb = new System.Text.StringBuilder();     // need to add mscorlib.dll
             char[] seps = { ' ',',', '\n','\r' };
            string [] splitAr  = s.Split(seps);
            for(int i=0;i<splitAr.Length;i++){
                sb.Append(StringReverse(splitAr[i]));
                sb.Append(" ");
            }
             return sb.ToString();
        }
         private void button1_Click(object sender, System.EventArgs e) {
            string sr = "test 123 ricka";
            MessageBox.Show("Reverse each char should yield  \n \"akcir 321 tset\"","result = \"" + StringReverse(sr) + "\"");
            MessageBox.Show("Double Reverse should yield original string \n \"" + sr + "\"",
                "result = \"" + StringReverse(StringReverse(sr))+ "\"");
             MessageBox.Show("should reverse order of strings \n & preserve each string \n original str = \"" + sr + "\"",
                "result = \"" + reverseEachString(StringReverse(sr))+ "\"");
         }

----------


## DNA7433

Yeah, but do you know the trick for reversing the string without using extra memory buffers?

----------


## Sir Loin

Wouldn't just cycling through a string suffice? I'm new to C#, and I've got this code. 


VB Code:
private string strReverse(string Expression)
        {
            string result = null;
             for (int i = 1; i <= Expression.Length; i++)
            {
                result += Expression.Substring(Expression.Length - i, 1);
            }
            return result;
        }

It works, but is this a bad programming practice or something? It seems too simple for it to be good.

Thanks,
Sir Loin

----------


## DNA7433

Since strings are immutable, Sir Loin, inside your for loop a new string object is created and assigned back into result every time that line executes.  Using a StringBuilder will be much faster.

----------


## Sir Loin

You mean like this?



```
        private string strReverse(string Expression)
        {
            StringBuilder result = null;

            for (int i = 1; i <= Expression.Length; i++)
            {
                result.Append(Expression.Substring(Expression.Length - i, 1));
            }
            return result.ToString();
        }
```

What is the StringBuilder class, what are its pro's and con's?

Thanks,
Sir Loin

----------


## DNA7433

```
private string StrReverse(string expression)
{
    StringBuilder result = new StringBuilder();

    for (int i = 1; i <= expression.Length; i++)
    {
        result.Append(expression[Expression.Length - i]);
    }

    return result.ToString();
}
```

You need to create a new instance of StringBuilder to use it.  Also, Substring will return a new string object and since all you need is a single character you can simply use indexing.

----------


## THEROB

> no need for a demo project , just copy the function and paste to your application to use.
> this carries out the same function as strReverse does in VB.
> 
> VB Code:
> [COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
>         {
>             MessageBox.Show(StringReverse("test 123"));
>         }
>          [COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
> ...



This speeds the code up:


```
        private string StringReverse(string ToReverse)
        {
            Array arr = ToReverse.ToCharArray();
            Array.Reverse(arr); // reverse the string
            char[] c = (char[])arr;
            return (new string(c));
        }
```

----------

