Created
December 17, 2012 16:50
-
-
Save dazfuller/4319745 to your computer and use it in GitHub Desktop.
Code snippets for C# string reverse blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExtensions | |
{ | |
public static string ReverseString(this string input) | |
{ | |
return new string(input.Reverse().ToArray()); | |
} | |
} | |
void Main() | |
{ | |
Console.WriteLine("Hello World!".ReverseString()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Give these a go in LinqPad (http://www.linqpad.net/)