#include<iostream>
#include<stdio.h>
using namespace std;
class Solution
{
public:
void Repalce_Space(char *str ,int length)
{
int space_count = 0;
int o_length = 0;
int n_length;
int i = 0;
while(str[i] != '\0')
{
o_length++;
if(str[i] ==' ')
{
space_count++;
}
i++;
}
n_length = o_length + 2*space_count;
char *p1 = str + o_length;
char *p2 = str + n_length;
while(p1 != p2)
{
if(*p1 ==' ')
{
*p2--='0';
*p2--='2';
*p2--='%';
}
else
{
*p2-- = *p1;
}
--p1;
}
}
};
int main()
{ Solution solution;
int length = 1024;
int i=0;
char str[length] = {"We Are Happy"};//不能直接用字符串給數組賦值,得加大括號。
solution.Repalce_Space(str ,length);
while(str[i]!='\0')
{
printf("%c",str[i]);
i++;
}
return 0;
}
劍指offer 替換空格 c++
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.