字符串反转问题
2014-03-10 00:04
232 查看
#include <iostream> /* Funtion: 字符串的反转操作 例如 “I am a student”反转为" student a am I"; */ void translate(const char* src, char* des) { int len = strlen(src); char* p = (char*)src + len - 1; int count = 0; while (len--) { p--; if (*p == ' ') { //copy word for (int i = 0; i < count; i++) *des++ = *(p + i + 1); *des++ = ' '; count = 0; } else { count++; } } for (int i = 0; i < count; i++) *des++ = *(p + i + 1); *des = '\0'; } int _tmain(int argc, _TCHAR* argv[]) { const char* src = "Im a student!!"; char* des = new char[strlen(src) + 1]; translate(src, des); std::cout << des << std::endl; return 0; }