Advertisement
yahorrr

Untitled

May 15th, 2022
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 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 + 1) / 2];
  29.             char[] endBufferArray = new char[source.Length / 2];
  30.  
  31.             int bufferIndex = 0;
  32.  
  33.             for (int i = 1; i < source.Length; i += 2, bufferIndex++)
  34.             {
  35.                 startBufferArray[bufferIndex] = sourceArray[i];
  36.             }
  37.  
  38.             bufferIndex = 0;
  39.  
  40.             for (int i = 0; i < source.Length; i += 2, bufferIndex++)
  41.             {
  42.                 endBufferArray[bufferIndex] = sourceArray[i];
  43.             }
  44.  
  45.             string startBuffer = startBufferArray.ToString();
  46.             string endBuffer = endBufferArray.ToString();
  47.  
  48.             return startBuffer + endBuffer;
  49.         }
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement