1. Start a new project (File -> New Project -> Console Application -> OK)
2. In the main function we are going to just read a line and call our reverse function to print it to the screen, so we'll use this code:
static void Main(string[] args)
{
string readline;
readline = ReverseString(Console.ReadLine());
Console.WriteLine(readline);
}
3. Now we need to put in the ReverseString function. Under the Main function ending curly brace put this code in:
static string ReverseString(string str)
{
string newstr = "";
for (int i = 1; i < str.Length; i++)
{
newstr = newstr + str[str.Length - i];
}
newstr = newstr + str[0];
return newstr;
}
This is a simple algorithm I designed to reverse the string.
4. Run the program (Press Ctrl+F5) and type in a line and press enter. It will print the string back to you! Now you can use the ReverseString function anywhere, and you dont just have to use it for this purpose!
Hope this helps you reverse string :)
*Note: There are better ways to reverse the string, but this just came off the top of my head and works.


Sign In
Create Account


Back to top









