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 not 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 dont 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.

5 Answers

3
Idle_Mind On

I haven't examined your string reversing algorithm at all, but in these two lines:

Console.ReadLine();
answer = aString.reverseStr("");

You get a string from the user with ReadLine(), which you simply throw away since you didn't store it in a variable. Then you are asking reverseStr() to reverse a blank string, which is probably not what you want.

It should look more like:

string response = Console.ReadLine();
answer = aString.reverseStr(response);

Or you could combine the two in one line:

answer = aString.reverseStr(Console.ReadLine());
2
David On

You don't do anything with the user's input:

Console.ReadLine();

And you're reversing an empty string:

answer = aString.reverseStr("");

Store the user's input and reverse that instead:

var input = Console.ReadLine();
answer = aString.reverseStr(input);
1
ArunPratap On

you can reverse string in main method like this

 public static void Main(string[] args)
    {
        Console.WriteLine("enter a string");
        string stng =Console.ReadLine();
        char[] revArray = stng.ToCharArray();
        Array.Reverse( revArray );
        Console.WriteLine(revArray);
    }
0
Olivier Jacot-Descombes On

You are feeding the method with an empty string aString.reverseStr(""). You must read the user input into a variable and pass this one to the reverse method.

string input = Console.ReadLine();
answer = aString.reverseStr(input);

You can remove the j variable as you are not using it. The Console.WriteLine("String in reverse: "); should be in the main method, where all the other console input/output is. The reverseStr method should only do the reversing stuff and not have side effects. This would be cleaner.

0
mtv On

Unfortunately, there are no built in functions to do that. You could use a char array and reverse the array and then get the string back.

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            string output;
            MyStr aString = new MyStr();
            Console.WriteLine("enter a string");
           input = Console.ReadLine();
            output = aString.reverseStr(input);
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }

class MyStr
{
    public string reverseStr(string Myname)
    {
        char[] arr = Myname.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

}
}

Alternatively, instead of MyStr class, you could define an extension for string. And your calling statement will be as simple as

string output = input.Reverse();

Extension method as follows and it can be used everywhere without instantiating a class by just using string variable.

public static class StringUtility
{
    public static string Reverse(this string input)
    {
        char[] arr = input.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }
}