You are viewing a single comment's thread. Return to all comments →
c++ solution (tried to use as few built-in functions as possible):
int getSqrtOfStringLength(string& s) { int sqrt = 1; for (; sqrt * sqrt < s.size(); sqrt++) {} if (sqrt * sqrt == s.size()) { return sqrt; } return sqrt - 1; } string removeWhitespaces(string& s) { string noWhitespaces; for (char character : s) { if (character != ' ') { noWhitespaces += character; } } return noWhitespaces; } string encryption(string s) { s = removeWhitespaces(s); int sqrt = getSqrtOfStringLength(s); int rowLength = sqrt, columnLength = sqrt; if (rowLength * columnLength < s.size()) { columnLength++; } if (rowLength * columnLength < s.size()) { rowLength++; } string finalAnswer; for (int addition = 0; addition < columnLength; addition++) { for (int rowIndex = 0; rowIndex < rowLength; rowIndex++) { int requiredStringPosition = rowIndex * columnLength + addition; if (requiredStringPosition >= s.size()) { continue; } finalAnswer += (s.at(requiredStringPosition)); } finalAnswer += ' '; } return finalAnswer; }
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →
c++ solution (tried to use as few built-in functions as possible):