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));
- }
- char[] sourceArray = source.ToCharArray();
- char[] startBufferArray = new char[(source.Length + 1) / 2];
- char[] endBufferArray = new char[source.Length / 2];
- int bufferIndex = 0;
- for (int i = 1; i < source.Length; i += 2, bufferIndex++)
- {
- startBufferArray[bufferIndex] = sourceArray[i];
- }
- bufferIndex = 0;
- for (int i = 0; i < source.Length; i += 2, bufferIndex++)
- {
- endBufferArray[bufferIndex] = sourceArray[i];
- }
- string startBuffer = startBufferArray.ToString();
- string endBuffer = endBufferArray.ToString();
- return startBuffer + endBuffer;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement