Advertisement
NPSF3000

StringReversal

Jul 13th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace StringReversal
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //Note how I don't bother to use a variable?
  10.             //This is a nasty hack that knows the compiler will intern the value.
  11.             //One of the many downsides of hacking the BCL.
  12.             while (string.IsNullOrWhiteSpace(Console.ReadLine()))
  13.             {
  14.                 Console.WriteLine("he\0llo!");  
  15.                 "he\0llo!".ReverseInPlace();
  16.             }
  17.         }
  18.     }
  19.  
  20.     public static class Helper
  21.     {
  22.         //Does not support multi-char values.
  23.         public static unsafe void ReverseInPlace(this string str)
  24.         {
  25.             fixed (char* pfixed = str)
  26.                 for (int i = 0, ii = str.Length - 1; i < str.Length / 2; i++, ii--)
  27.                 {
  28.                     var p1 = (char*)pfixed + i;
  29.                     var p2 = (char*)pfixed + ii;
  30.                     var temp = *p1;
  31.                     *p1 = *p2;
  32.                     *p2 = temp;
  33.                 }
  34.         }
  35.     }
  36. }
  37.  
  38. //Absolutely *NO* Warranty Provided.  Do *NOT* use.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement