Advertisement
yahorrr

Untitled

May 15th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ShuffleCharacters
  4. {
  5.     public static class StringExtension
  6.     {
  7.         /// <summary>
  8.         /// Shuffles characters in source string according some rule.
  9.         /// </summary>
  10.         /// <param name="source">The source string.</param>
  11.         /// <param name="count">The count of iterations.</param>
  12.         /// <returns>Result string.</returns>
  13.         /// <exception cref="ArgumentException">Source string is null or empty or white spaces.</exception>
  14.         /// <exception cref="ArgumentException">Count of iterations is less than 0.</exception>
  15.         public static string ShuffleChars(string source, int count)
  16.         {
  17.             if (string.IsNullOrWhiteSpace(source))
  18.             {
  19.                 throw new ArgumentException("Source string is null or empty or white spaces.", nameof(source));
  20.             }
  21.  
  22.             if (count < 0)
  23.             {
  24.                 throw new ArgumentException("Count of iterations is less than 0", nameof(count));
  25.             }
  26.  
  27.             char[] sourceArray = source.ToCharArray();
  28.             char[] startBufferArray = new char[source.Length / 2];
  29.             char[] endBufferArray = new char[(source.Length + 1) / 2];
  30.  
  31.             int bufferIndex = 0;
  32.             int r = 0;
  33.  
  34.             for (int i = 0; i < count; i++)
  35.             {              
  36.                 for (int k = 1; i < source.Length; i += 2, bufferIndex++)
  37.                 {
  38.                     startBufferArray[bufferIndex] = sourceArray[k];
  39.                 }
  40.  
  41.                 bufferIndex = 0;
  42.  
  43.                 for (int k = 0; i < source.Length; i += 2, bufferIndex++)
  44.                 {
  45.                     endBufferArray[bufferIndex] = sourceArray[k];
  46.                 }
  47.  
  48.                 for (int j = 0; j < source.Length / 2; j++)
  49.                 {
  50.                     sourceArray[j] = startBufferArray[j];
  51.                 }
  52.  
  53.                 r = 0;
  54.  
  55.                 for (int e = source.Length / 2; e < source.Length; e++, r++)
  56.                 {
  57.                     sourceArray[e] = startBufferArray[r];
  58.                 }
  59.             }
  60.  
  61.             string startBuffer = new string(startBufferArray);
  62.             string endBuffer = new string(endBufferArray);
  63.  
  64.             return startBuffer + endBuffer;
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement