I have used C# for 2 months and I have to write a program that will reverse a string(needs to be in a method). I need to call that method in the other class that has a main method. I have done this but when the program runs it just asks me to enter a string and it doesn't display a string in reverse please help.
Here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign3
{
class MyStr
{
public string reverseStr(string Myname)
{
string temp = "";
int i, j;
Console.WriteLine("String in reverse: ");
for (j = 0, i = Myname.Length - 1; i >= 0; i--, j++)
temp += Myname[i];
return temp;
}
}
}
and the next class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign3
{
class Test
{
static void Main(string[] args)
{
string answer;
MyStr aString = new MyStr();
//MyStr.reverseStr(string temp);
//Console.WriteLine(aString);
Console.WriteLine("enter a string");
Console.ReadLine();
answer = aString.reverseStr("");
Console.WriteLine(answer);
Console.WriteLine(aString.reverseStr(i);
Console.ReadLine();
}
}
}
I don't know how to get this method to output the string in reverse. Please help me understand what I did wrong and please guide me in the right direction to correct this.