Click here to Skip to main content
15,888,158 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
sample string: A3148579
i tried this code:

What I have tried:

C#
public static string Reverse(string s)
    {
        char[] charArray = s.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }


the result is 9758413A
but i want 798514A3
for understand
thanks everyone
Posted
Updated 26-Jul-20 6:21am

1 solution

What you are talking about is pretty much "swapping byte values" in effect.
Try this:
C#
private static string Reverse(string str)
    {
    StringBuilder sb = new StringBuilder(str.Length);
    for (int i = str.Length; i > 0; i -= 2)
        {
        sb.Append(str[i - 2]);
        sb.Append(str[i - 1]);
        }
    return sb.ToString();
    }
 
Share this answer
 
v2
Comments
BillWoodruff 27-Jul-20 0:28am    
+5 Two calls to 'Append to avoid use of 'Substring creating new strings !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900