Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ShuffleCharacters
- {
- public static class StringExtension
- {
- /// <summary>
- /// Shuffles characters in source string according some rule.
- /// </summary>
- /// <param name="source">The source string.</param>
- /// <param name="count">The count of iterations.</param>
- /// <returns>Result string.</returns>
- /// <exception cref="ArgumentException">Source string is null or empty or white spaces.</exception>
- /// <exception cref="ArgumentException">Count of iterations is less than 0.</exception>
- public static string ShuffleChars(string source, int count)
- {
- if (string.IsNullOrWhiteSpace(source))
- {
- throw new ArgumentException("Source string is null or empty or white spaces.", nameof(source));
- }
- if (count < 0)
- {
- throw new ArgumentException("Count of iterations is less than 0", nameof(count));
- }
- string bufferStart = string.Empty;
- string bufferEnd = string.Empty;
- for (int i = 0; i < count; i++)
- {
- for (int j = 1; j < source.Length; j += 2)
- {
- bufferStart = string.Concat(bufferStart, source[j]);
- }
- for (int k = 0; k < source.Length; k += 2)
- {
- bufferEnd = string.Concat(bufferEnd, source[k]);
- }
- source = string.Concat(bufferStart, bufferEnd);
- }
- return source;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement