Untitled
unknown
c_cpp
3 years ago
888 B
7
Indexable
string encryption(string s) {
int row = floor(sqrt(s.length()));
int column = ceil(sqrt(s.length()));
if (row * column < s.length())
{
row = column;
}
char arr[row][column];
int counter = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (counter >= s.length())
{
arr[i][j] = ' ' ;
break;
}
arr[i][j] = s.at(counter);
counter++;
}
}
string ans;
for (int i = 0; i < column; i++)
{
for (int j = 0; j < row; j++)
{
if (arr[j][i] != ' ' && arr[j][i] != '\0')
ans.push_back(arr[j][i]);
}
if (i != column - 1)
ans.push_back(' ');
}
return ans;
}Editor is loading...