Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
hi every body
i have string like this "12345"
i want to reflect it to be "54321"
what is the code in this situation
thanks for you
M. El-Wehishy
Posted
Comments
Pikoh 1-Jul-15 9:27am    
That seems homework. It is an extremely easy question,try to do it yourself and if you got stuck, come here for help.
Sergey Alexandrovich Kryukov 1-Jul-15 12:54pm    
The best solution so far is Solution 2. Main thing to remember: strings are immutable, so, to compose a string from data, don't use + (concatenation) on strings, use the mutable type System.Text.StringBuilder.
(This is not a value question! You are way too lazy.)
—SA

The best solution so far is Solution 2.

If you want to solve this problem on a lower level, main thing to remember: strings are immutable, so, to compose a string from data, don't use + (concatenation) on strings, use the mutable type System.Text.StringBuilder:
https://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx[^].

Another approach would be: create a fixed-length array of characters (you know the length of it, because you know the length of the input string), fill it in with characters of the input string in reverse order in a loop, create a new string out of this array.

—SA
 
Share this answer
 
v2
You can use Array.Reverse[^] to do this.
C#
string str="12345";
char[] chrArray = str.ToCharArray();
string result = Array.Reverse( chrArray );

Hope, it helps :)
 
Share this answer
 
v2
Comments
Animesh Datta 1-Jul-15 11:17am    
My 5!
Suvendu Shekhar Giri 1-Jul-15 11:41am    
Thanks :)
Sergey Alexandrovich Kryukov 1-Jul-15 12:52pm    
My 5 — SA
Afzaal Ahmad Zeeshan 5-Jul-15 4:30am    
Interesting way implemented - 5ed!
Hi,

Here's an article that demonstrates how to do a simple string reverse:
Reverse of a string without using the Reverse function in C# and VB[^]

Another article demonstrates a few different ways to accomplish this:
Various string reversal algorithms in C#[^]

... hope it helps.
 
Share this answer
 
thanks very much for everyone helped me in this simply question
 
Share this answer
 

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