Advertisement
yahorrr

Untitled

May 14th, 2022
762
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.             string bufferStart = string.Empty;
  28.             string bufferEnd = string.Empty;
  29.  
  30.             for (int i = 0; i < count; i++)
  31.             {
  32.                 for (int j = 1; j < source.Length; j += 2)
  33.                 {
  34.                     bufferStart = string.Concat(bufferStart, source[j]);
  35.                 }
  36.  
  37.                 for (int k = 0; k < source.Length; k += 2)
  38.                 {
  39.                     bufferEnd = string.Concat(bufferEnd, source[k]);
  40.                 }
  41.  
  42.                 source = string.Concat(bufferStart, bufferEnd);
  43.             }
  44.  
  45.             return source;
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement