#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
void ReplaceBlack(char str[], int lengh)
{
if (str == NULL || lengh < 0)
{
return;
}
int i = 0;
int k = 0;
while (str[i] != '\0')
{
if (str[i] == ' ')
{
++k;
}
++i;
}
int p1 = lengh , p2 = p1 + k * 2;
while (p1 >= 0)
{
if (str[p1] != ' ')
{
str[p2--] = str[p1];
}
else
{
str[p2--] = '0';
str[p2--] = '2';
str[p2--] = '%';
}
p1--;
}
}
void main()
{
char a[50] = "we are happy.";//{ 'w', ' ', 'a', 'H', 'a', 'p', 'y', '!' };
int n = strlen(a);
ReplaceBlack(a,n);
for (int i = 0; i < n+4; i++)
{
cout << a[i];
}
cout << endl;
}
剑指offor面试题4:替换空格
最新推荐文章于 2024-02-27 18:26:29 发布