Skip to content

Instantly share code, notes, and snippets.

@westonal
Last active August 29, 2015 14:19
Show Gist options
  • Save westonal/2f43bf31994a9f07b4ec to your computer and use it in GitHub Desktop.
Save westonal/2f43bf31994a9f07b4ec to your computer and use it in GitHub Desktop.
Reverse string...
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.Collections.Generic;
namespace ReverseString
{
[TestClass]
public class ReverseStringTests
{
[TestMethod]
public void Empty()
{
Assert.AreEqual("", Reverse(""));
}
[TestMethod]
public void SingleChar()
{
Assert.AreEqual("A", Reverse("A"));
}
[TestMethod]
public void TwoChars()
{
Assert.AreEqual("BA", Reverse("AB"));
}
[TestMethod]
public void ThreeChars()
{
Assert.AreEqual("CBA", Reverse("ABC"));
}
[TestMethod]
public void LesMis_no_accent()
{
Assert.AreEqual("selbaresiM seL", Reverse("Les Miserables"));
}
[TestMethod]
public void LesMisCopied()
{
Assert.AreEqual("selbarésiM seL", Reverse("Les Misérables"));
}
[TestMethod]
public void LesMis()
{
Assert.AreEqual("selbare\u0301siM seL", Reverse("Les Mise\u0301rables"));
}
string Reverse2(string str)
{
var l = str.ToCharArray().ToList();
l.Reverse();
return String.Concat(l.ToArray());
}
string Reverse(string str)
{
var e = StringInfo.GetTextElementEnumerator(str);
var l = new List<string>();
while (e.MoveNext())
{
l.Add(e.GetTextElement());
}
l.Reverse();
return string.Concat(l);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment