-5

i'm am trying to make a program that reverses text in C#. I'm am new to C# and am not quite sure how to do this. I know i have to get the input from the textbox and convert it but i don't know how to do that. Some of the websites i looked at are either for VB or have been closed.

i would also like to have another textbox display the length of the word

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        textBox2.Text = textBox1.Text;

        textBox3.Text = textBox1.Text + "4";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox2.Text = " ";
        textBox3.Text = " ";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }


}
}

I was able to find this code but am unsure how to make it fit with mine

private void btnReverseString_Click(System.Object sender, System.EventArgs e)
{
   string OriginalString = txtOriginalString.Text;
   string ReverseString = "";
   for (int i = 0; i <= OriginalString.Length - 1; i++)
     {
        ReverseString += OriginalString[OriginalString.Length - 1 - i];
     }
   txtReverseString.Text = ReverseString;
}
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • I agree it's a duplicate, I'm not voting to close as dupe only because **there isn't a satisfactory answer** in that post. If anyone else find a better dupe I'll vote to close. – Adriano Repetti Aug 21 '17 at 13:47

3 Answers3

6

At first some naive solutions:

var reversedText = new String(text.Reverse().ToArray());

Or:

var reversedText = String.Concat(text.Reverse());

Or:

var chars = text.ToCharArray();
Array.Reverse(chars);
var reversedText = new String(chars);

However they dont't work well with Unicode because a grapheme may be composed by more than one char (and one Unicode code point may be encoded as more than one code unit - which is the correct name for char type). One example?

string text = "à";
Debug.Assert(text.Length == 2);

It's 2 because it's composed by character +U0041 LATIN CAPITAL LETTER A and +U0300 COMBINING GRAVE ACCENT. If you simply reverse the string then you won't obtain the right text because with combining characters, encoded text and Unicode surrogates the order is important and must be preserved. I'd suggest to read How can I perform a Unicode aware character by character comparison?.

Slightly better version:

var elements = new List<string>();
var enumerator = StringInfo.GetTextElementEnumerator(text);
while (enumerator.MoveNext())
    elements.Add((string)enumerator.Current);

var reversedText = String.Concat(elements.Reverse());

It's still far to be perfect, you may want to wrap that IEnumerator inside a IEnumerable<string> using an hypothetical AsEnumerable() extension method to write this:

var elements = StringInfo.GetTextElementEnumerator(text).AsEnumerable();
var reversedText = String.Concat(elements.Reverse());

Note that it's not the final solution because there are some code-points which must be in specific positions and they have different and special meanings (for example RTL marker). Another real-world example I love to cite: Why are emoji characters like ‍‍‍ treated so strangely in Swift strings?

Xcoder
  • 1,433
  • 3
  • 17
  • 37
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

The good old C++ style

string input = "hello world"
string output = string.Empty;

for(int i = input.Count(); i >= 0; i--)
{
    output += input[i];
}

adapted to your code:

private void btnReverseString_Click(System.Object sender, System.EventArgs e)
{
   string OriginalString = txtOriginalString.Text;
   string ReverseString = "";
   for (int i = OriginalString.Length; i >= 0; i--)
     {
        ReverseString += OriginalString[i];
     }
   txtReverseString.Text = ReverseString;
}

You can also use Linq : t.Reverse(). This gives you an enumerable, so you'll need to instantiate a new string from that IEnumerable<char>

Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48
-1
 private static void ReverseFirst()
    {
        Console.WriteLine("Reverse your name game!");
        Console.Write("Enter your first name: ");
        string firstName;
        firstName = Console.ReadLine();

        char[] reverseFirst = firstName.ToCharArray();
        Array.Reverse(reverseFirst);
        foreach(char i in reverseFirst)
        {
         Console.Write(i);
        }
    }

Here is a method in C# that takes the input of your first name, and returns it reversed.(You can then substitute your own variables) This is my first answer on Stack Overflow so I hope this helps!

  • 4
    How is this answer different from approaches mentioned in the above answers? – vCillusion Jun 11 '18 at 23:47
  • It doesn't really _return_ it reversed, it just prints it reversed. There's a difference. Methods should do their job and return a result for the caller to print if they want to, or programmatically manipulate further. Likewise, reading from the console isn't relevant to reversing. That should also be in the caller, not part of the reversing logic. – ggorlen Oct 05 '22 at 20:43