=0){ rev=rev+myStr[len]; len--;} Example You can try running the following code to reverse a string in C#. Live demonstration usingSystem;cl"/> =0){ rev=rev+myStr[len]; len--;} Example You can try running the following code to reverse a string in C#. Live demonstration usingSystem;cl">
Home > Article > Backend Development > C# program to reverse string
Our example string is -
myStr = "Tom";
To reverse the string, first find the length of the string −
// find string length int len; len = myStr.Length - 1;
Now , use a while loop until the length is greater than 0 -
while (len >= 0) { rev = rev + myStr[len]; len--; }
You can try running the following code to reverse a string in C#.
Real-time demonstration
using System; class Demo { static void Main() { string myStr, rev; myStr = "Tom"; rev =""; Console.WriteLine("String is {0}", myStr); // find string length int len; len = myStr.Length - 1; while (len >= 0) { rev = rev + myStr[len]; len--; } Console.WriteLine("Reversed String is {0}", rev); Console.ReadLine(); } }
String is Tom Reversed String is moT
The above is the detailed content of C# program to reverse string. For more information, please follow other related articles on the PHP Chinese website!