您的位置:首页 > 其它

去除字符串中的空格

2016-03-21 22:28 274 查看
去除字符串中的空格,并返回修改后的字符串。

要求时间复杂度O(N),空间复杂度O(1)。

#include "stdafx.h"
#include <process.h>
#include <iostream>

using namespace std;

void DeleteSpace(char* pStr)
{
int j = 0;
for (int i=0; pStr[i]!='\0'; i++)
{
if (pStr[i] != ' ')
{
if (i != j)
{
pStr[j] = pStr[i];
}

j++;
}
}
pStr[j++] = '\0';
}

int _tmain(int argc, _TCHAR* argv[])
{
char str[] = "Hello  w o rl d   !";
cout << "Before: " << str << endl;
DeleteSpace(str);
cout << "After: " << str << endl;
system("pause");

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