C# program to reverse string

WBOY
Release: 2023-09-05 19:09:03
forward
1306 people have browsed it

C# 反转字符串的程序

Our example string is -

myStr = "Tom";
Copy after login

To reverse the string, first find the length of the string −

// find string length
int len;
len = myStr.Length - 1;
Copy after login

Now , use a while loop until the length is greater than 0 -

while (len >= 0) {
   rev = rev + myStr[len];
   len--;
}
Copy after login

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

Output

String is Tom
Reversed String is moT
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!