
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Reverse a String
Our sample string is −
myStr = "Tom";
To reverse the string, firstly 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 to run the following code to reverse a string in C#.
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
Advertisements