What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8801 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Reverse a string ...
Madhu.b.rokkam

Reverse a string

 Code Snippet posted by: Madhu.b.rokkam | Posted on: 2/21/2011 | Category: C# Codes | Views: 853 | Status: [Member] [MVP] | Points: 40 | Alert Moderator   


We can reverse a string as below

       public string Reverse(string inputValue)

{
char[] charArr = inputValue.ToCharArray();
Array.Reverse(charArr);
return new string(charArr);
}

or


public static string Reverse(string inputValue)

{
StringBuilder sb = new StringBuilder();
for (int i = inputValue.Length-1; i >= 0; i--)
{
sb.Append(inputValue);
}
return sb.ToString();
}


its simple..

Thanks and Regards
Madhu
Found interesting? Add this to:


 Responses

Tripati.patro
Posted by: Tripati.patro | Posted on: 2/21/2011 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

Good One.

B.Tripati Patro
Oracle Certified Associate

Madhu.b.rokkam
Posted by: Madhu.b.rokkam | Posted on: 2/21/2011 | Level: Bronze | Status: [Member] [MVP] | Points: 10 | Alert Moderator 

Thanks Tripati

Thanks and Regards
Madhu

T.saravanan
Posted by: T.saravanan | Posted on: 2/22/2011 | Level: Silver | Status: [Member] [MVP] | Points: 10 | Alert Moderator 

Hi Madhu,

Nice Try...
Change the loop variable 'i' because in our website 'i' is consider as italic format.So change
for (int i = inputValue.Length-1; i >= 0; i--) to for (int x = inputValue.Length-1; x >= 0; x--)

Try this way to reverse a string...
string s = "ABCDEF";

StringBuilder sb = new StringBuilder();
foreach(char c in s)
{
sb.Insert(0,c);
}
Console.WriteLine(sb);


Cheers :)



Thanks,
T.Saravanan

Madhu.b.rokkam
Posted by: Madhu.b.rokkam | Posted on: 2/22/2011 | Level: Bronze | Status: [Member] [MVP] | Points: 10 | Alert Moderator 

thanks for ur inputs saravanan

Thanks and Regards
Madhu

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 2:37:51 AM