One of my friend asked me to write a program that encode a message and make it difficult to understand, kind or secret message.
Message: Hi I am Sunil
First of all there should not be any white space, so our message now looks like “HiIamSunil”. Now we have to make a rectangle form of the message where rows can not be greater than columns.
HiIa
mSun
il
Now the encoded message is “Hmi iSl Iu an”. There should be a white space between every word.
Other examples:
Input: Chillout
Output: Clu hlt io
Input: Good day Sunil
Output: Gdu oan oyi dSl
Here is my solution:
string str = “Good day Sunil”.Replace(” “, “”);
int len = str.Length;
double result = Math.Sqrt(len);
double numCol = Math.Ceiling(result);
double numRow = Math.Floor(result);
int colNo = (int)numCol;
char[] arr = str.ToCharArray();
string encodeMsg = string.Empty;
string temp;
for (int i = 0; i < colNo; ++i)
{
temp = string.Empty;
for (int j = i; j < len; )
{
temp += arr[j];
j += colNo;
}
encodeMsg += temp + ” “;
}
Console.WriteLine(encodeMsg);
Console.ReadLine();