You are viewing a single comment's thread. Return to all comments →
C++ solution
string encryption(string s) { string s_without_spaces = ""; for (size_t i = 0; i < s.size();++i ) { if (!isspace(s[i])) { s_without_spaces += s[i]; } } size_t length = s_without_spaces.size(); double t=sqrt(static_cast(length)); size_t cols =static_cast( ceil(t)); size_t rows = static_cast(floor(t)); if (cols * rows < length) { ++rows; } string encrypt =""; size_t k = 0; while (k < cols) { for (size_t j = 0; j < rows; ++j) { if ((j * cols + k) < length) { encrypt += s_without_spaces[j * cols + k]; } } encrypt += " "; ++k; } return encrypt; }
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
string encryption(string s) { string s_without_spaces = ""; for (size_t i = 0; i < s.size();++i ) { if (!isspace(s[i])) { s_without_spaces += s[i]; } } size_t length = s_without_spaces.size(); double t=sqrt(static_cast(length)); size_t cols =static_cast( ceil(t)); size_t rows = static_cast(floor(t)); if (cols * rows < length) { ++rows; } string encrypt =""; size_t k = 0; while (k < cols) { for (size_t j = 0; j < rows; ++j) { if ((j * cols + k) < length) { encrypt += s_without_spaces[j * cols + k]; } } encrypt += " "; ++k; } return encrypt; }