-4

I want to know how to reverse a string in C# without using any .NET built-in methods.

My initial code:

Console.WriteLine("Please enter a string");
string myString = Console.ReadLine();  

The idea is to reverse the string named myString, which we are getting via the User's Console Input.

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
Tayyab Yasin
  • 37
  • 1
  • 10
  • sounds like a homework, what have _you_ tried? what the difficulties that _you_ are facing – styx Dec 02 '18 at 09:54
  • Use another language. You can't work with .Net without using any of the methods built in the framework. – Zohar Peled Dec 02 '18 at 09:54
  • @ZoharPeled Answer is posted check please as it is reversing the given string without using built-in method. – Tayyab Yasin Dec 02 '18 at 09:59
  • 1
    arrays are a part of the .Net framework. A for loop is translated by the c# compiler to work with an IEnumerator (which is a part of the .Net framework) and so on. So the answer you posted is using built in methods. You are talking with programmers - we like nitpicking. – Zohar Peled Dec 02 '18 at 10:03

5 Answers5

5

If you are trying to reverse a string that contains only English letters (I presume since your answer contains no methods to handle other letters), you could simplify your code by doing something like this:

private static string ReverseString(string myString)
{
    string reversedString = string.Empty;

    for (int i = myString.Length - 1; i >= 0 ; i--)
    {
        reversedString += myString[i];
    }

    return reversedString;
}

This method, obviously, doesn't contain any way to handle non-English letters, it is just a simplification of your answer.

Alessio Raddi
  • 540
  • 1
  • 6
  • 20
0

We can convert the string to a char array and reverse the array. Given an array of size N, we only need to iterate in a loop for N/2 times while swapping the characters from the end of the array with the ones in the beginning. We can start from the beginning and ending characters, swap those and then move inwards with each iteration, until we reach the middle of the array.

    public static string Reverse(string input)
    {
        var inputArray = input.ToCharArray();
        var end = inputArray.Length / 2;

        for (int i = 0; i < end; i++)
        {
            var temp = inputArray[i];
            inputArray[i] = inputArray[inputArray.Length - i - 1];
            inputArray[inputArray.Length - i - 1] = temp;
        }

        var result = new string(inputArray);

        return result;
    }
baez
  • 3
  • 2
0
  public void reverse(string r)
            {
                string ans = "";
                for (int i = r.Length-1; i >= 0; i--)
                {
                    ans = ans + r[i];

                }


                Console.WriteLine(ans);
            }
-1

convert it to a character array, reverse, convert to string. (that is how i would do it in java).

-1

We can use the procedure below to reverse the String.

string myString = Console.ReadLine();
        char[] charStr = new char[str.Length];

        int k = charStr.Length - 1;
        for (int i=0;i<charStr.Length;i++)
        {
            int j = k;
            while (j>=0)
            {
                charStr[j] = str[i];
                j--;
            }
            k--;
        }

        foreach (char item in charStr)
        {
            Console.Write(item);
        }
        Console.WriteLine();
Tayyab Yasin
  • 37
  • 1
  • 10
  • Did you find the answer *after* posting the question? – Ofir Winegarten Dec 02 '18 at 09:59
  • 2
    This will fail miserably with anything containing non-English letters - like, for instance - ` בְּרֵאשִׁית, בָּרָא אֱלֹהִים, אֵת הַשָּׁמַיִם, וְאֵת הָאָרֶץ`. - This is the first sentence in the Hebrew bible (Christian old-testament) - and if you reverse it char by char you get all the little dots (which are Hebbew vowels) in the wrong places. – Zohar Peled Dec 02 '18 at 10:11
  • [This answer](https://stackoverflow.com/a/15111719/3094533) shows a correct way to reverse a string which might contain non-English letters. – Zohar Peled Dec 02 '18 at 10:16
  • @ZoharPeled but he said **without using any .NET built-in method** im guessing he didn't meant to **any** build in method, just not any **reverse method** (the linked answer is using the `Reverse()` method) – styx Dec 02 '18 at 10:20
  • The linked answer is not an attempt to not use reverse, it shows how to correctly handle strings that contain non-English letters. Two very different things. – Zohar Peled Dec 02 '18 at 10:28