您的位置:首页 > 其它

字符串反转问题

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: