Results 1 to 10 of 10

Thread: strReverse in C#

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    strReverse in C#

    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:
    1. [COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
    2.         {
    3.             MessageBox.Show(StringReverse("test 123"));
    4.         }
    5.  
    6.         [COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
    7.         {
    8.             Array arr = ToReverse.ToCharArray();
    9.             Array.Reverse( arr ); [COLOR=Green]// reverse the string[/COLOR]
    10.             [COLOR=Blue]char[/COLOR][] c = ([COLOR=Blue]char[/COLOR][])arr;
    11.             [COLOR=Blue]byte[/COLOR][] b = System.Text.Encoding.Default.GetBytes(c);
    12.             [COLOR=Blue]return[/COLOR] System.Text.Encoding.Default.GetString(b);
    13.         }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i don't know much mate but i've got this
    PHP Code:
          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...

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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));

  4. #4
    New Member
    Join Date
    Apr 2005
    Posts
    3

    Re: strReverse in C#

    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:
    1. string reverseEachString(string s){
    2.  
    3.             System.Text.StringBuilder  sb = new System.Text.StringBuilder();     // need to add mscorlib.dll
    4.  
    5.             char[] seps = { ' ',',', '\n','\r' };
    6.             string [] splitAr  = s.Split(seps);
    7.             for(int i=0;i<splitAr.Length;i++){
    8.                 sb.Append(StringReverse(splitAr[i]));
    9.                 sb.Append(" ");
    10.             }
    11.  
    12.             return sb.ToString();
    13.         }
    14.  
    15.         private void button1_Click(object sender, System.EventArgs e) {
    16.             string sr = "test 123 ricka";
    17.             MessageBox.Show("Reverse each char should yield  \n \"akcir 321 tset\"","result = \"" + StringReverse(sr) + "\"");
    18.             MessageBox.Show("Double Reverse should yield original string \n \"" + sr + "\"",
    19.                 "result = \"" + StringReverse(StringReverse(sr))+ "\"");
    20.  
    21.             MessageBox.Show("should reverse order of strings \n & preserve each string \n original str = \"" + sr + "\"",
    22.                 "result = \"" + reverseEachString(StringReverse(sr))+ "\"");
    23.  
    24.         }

  5. #5
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: strReverse in C#

    Yeah, but do you know the trick for reversing the string without using extra memory buffers?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  6. #6
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: strReverse in C#

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

    VB Code:
    1. private string strReverse(string Expression)
    2.         {
    3.             string result = null;
    4.  
    5.             for (int i = 1; i <= Expression.Length; i++)
    6.             {
    7.                 result += Expression.Substring(Expression.Length - i, 1);
    8.             }
    9.             return result;
    10.         }

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

    Thanks,
    Sir Loin

  7. #7
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: strReverse in C#

    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.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  8. #8
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: strReverse in C#

    You mean like this?

    Code:
            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

  9. #9
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: strReverse in C#

    Code:
    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.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  10. #10
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: strReverse in C#

    Quote Originally Posted by 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:
    1. [COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
    2.         {
    3.             MessageBox.Show(StringReverse("test 123"));
    4.         }
    5.  
    6.         [COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
    7.         {
    8.             Array arr = ToReverse.ToCharArray();
    9.             Array.Reverse( arr ); [COLOR=Green]// reverse the string[/COLOR]
    10.             [COLOR=Blue]char[/COLOR][] c = ([COLOR=Blue]char[/COLOR][])arr;
    11.             [COLOR=Blue]byte[/COLOR][] b = System.Text.Encoding.Default.GetBytes(c);
    12.             [COLOR=Blue]return[/COLOR] System.Text.Encoding.Default.GetString(b);
    13.         }

    This speeds the code up:
    Code:
            private string StringReverse(string ToReverse)
            {
                Array arr = ToReverse.ToCharArray();
                Array.Reverse(arr); // reverse the string
                char[] c = (char[])arr;
                return (new string(c));
            }
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width