Here's my solution. The method ShiftChunkChars takes a string and integer which depicts the amount of characters in each "chunk". Beware that this method does not work if the input string's length is not divisible by chunkSize.
string ShiftChunkChars(string original, int chunkSize)
{
char[] buffer = new char[chunkSize];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < original.Length; i += chunkSize)
{
char[] chars = original.Substring(i, chunkSize).ToCharArray();
Array.Copy(chars, 0, buffer, 1, chunkSize - 1);
buffer[0] = chars[chunkSize - 1];
sb.Append(buffer);
}
return sb.ToString();
}
Examples:
1.
void Main()
{
int chunkSize = 8;
string s = "11100110110100000111001101101000";
ShiftChunkChars(s, chunkSize);
}
Output:
01110011011010001011100100110100
// Original with spaces: 11100110 11010000 01110011 01101000
// Result with spaces: 01110011 01101000 10111001 00110100
2.
void Main()
{
int chunkSize = 4;
string s = "11100110110100000111001101101000";
ShiftChunkChars(s, chunkSize);
}
Output:
01110011111000001011100100110100
// Original with spaces: 1110 0110 1101 0000 0111 0011 0110 1000
// Result with spaces: 0111 0011 1110 0000 1011 1001 0011 0100