0

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.

2
  • Console.ReadLine returns what the user types. If you don't get this result you can't reverse anything. Commented Mar 2, 2019 at 23:34
  • Try echoing the input, enclosed in some delimiters. Commented Mar 2, 2019 at 23:42

5 Answers 5

3

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());
Sign up to request clarification or add additional context in comments.

Comments

2

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);

2 Comments

up one for mentioning the unused j variable. while that does not answering the question, it was definitely worth mentioning!
Sorry, I inadvertently updated your answer instead of mine :-(
1

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);
        Console.ReadLine();
    }

1 Comment

Add a Console.ReadLine(); at the end so we can see your correct result.
0

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);
    }
}

1 Comment

ok thank you, and is it possible to call a constructor the same way a method is called? is there a reason why I would put my code in a constructor instead of a method?
0

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.


You can pass a parameter through the constructor like this

class MyStr
{
    private string _name;

    // Constructor. Has same name as class. Doesn't have a return type.
    public MyStr(string name)
    {
        _name = name;
    }

    public string ReverseStr()
    {
        string temp = "";
        int i, j;

        for (j = 0, i = _name.Length - 1; i >= 0; i--, j++)
            temp += _name[i];

        return temp;
    }
}

And you would call it like this

var myStr = new MyStr(input);
string result = myStr.ReverseStr();

1 Comment

Thank you everyone who helped me with this, it is now working perfectly. How can I use a constructor method to take the input string as the parameter and assign it to the private string of my class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.