数据结构练习(18)左旋转字符串
http://zhedahht.blog.163.com/blog/static/2541117420073993725873/
#include <iostream> #include <cstring> using namespace std; void reverse_string(char *s, char *e) { if (s != nullptr && e != nullptr) { while (s < e) { char c = *e; *e = *s; *s = c; ++s, --e; } } } char* left_rotate_string(char *s, size_t n) { if (s != nullptr) { size_t len = static_cast<int>(strlen(s)); if (n > 0 && len > 0 && n < len) { reverse_string(s, s + n - 1); reverse_string(s + n, s + len - 1); reverse_string(s, s + len - 1); } } return s; } int main() { char b[200]; cin >> b; left_rotate_string(b, 2); cout << b; return 0; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------