=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

C# program to reverse string

WBOY
WBOYforward
2023-09-05 19:09:031319browse

C# 反转字符串的程序

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--;
}

Example

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();
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete