Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Created December 17, 2012 16:50
Show Gist options
  • Save dazfuller/4319745 to your computer and use it in GitHub Desktop.
Save dazfuller/4319745 to your computer and use it in GitHub Desktop.
Code snippets for C# string reverse blog post
void Main()
{
var printNum = true;
for (var i = 1; i <= 100; i++)
{
if (i % 3 == 0)
{
Console.Write("fizz");
printNum = false;
}
if (i % 5 == 0)
{
Console.Write("{0}buzz", printNum == false ? "-" : "");
printNum = false;
}
if (printNum == true)
{
Console.Write(i);
}
Console.Write("{0}", Environment.NewLine);
printNum = true;
}
}
public static class StringExtensions
{
public static string ReverseString(this string input)
{
var chr = input.ToCharArray();
var tmp = '0';
var i = 0;
var j = chr.Length - 1;
var end = chr.Length / 2;
while (i < end)
{
tmp = chr[i];
chr[i] = chr[j];
chr[j] = tmp;
i++;
j--;
}
return new string(chr);
}
}
void Main()
{
Console.WriteLine("Hello World!".ReverseString());
}
public static class StringExtensions
{
public static string ReverseString(this string input)
{
return new string(input.Reverse().ToArray());
}
}
void Main()
{
Console.WriteLine("Hello World!".ReverseString());
}
public static class StringExtensions
{
public static string ReverseSentance(this string input)
{
return String.Join(" ", input.Split(' ').Reverse());
}
}
void Main()
{
Console.WriteLine("Ignoring The Voices".ReverseSentance()); // Voices The Ignoring
}
public static class StringExtensions
{
public static string ReverseWord(this string input)
{
return new string(input.Reverse().ToArray());
}
public static string ReverseWordsInSentance(this string input)
{
return String.Join(" ", input.Split(' ').Select(n => n.ReverseWord()));
}
}
void Main()
{
Console.WriteLine("Ignoring The Voices".ReverseWordsInSentance()); // gnirongI ehT secioV
}
@dazfuller
Copy link
Author

Give these a go in LinqPad (http://www.linqpad.net/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment