-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceBlacnk.cpp
More file actions
41 lines (38 loc) · 943 Bytes
/
ReplaceBlacnk.cpp
File metadata and controls
41 lines (38 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*请实现一个函数,把字符串中的每个空格替换成“%20”,例如输入“I am ok”,,则输出“I%20am%20ok”。*/
#include<iostream>
#include<string>
using namespace std;
char* replaceBlacnk(char * str,int len)
{
if(str==NULL||len<=0)
return NULL;
int blackCnt=0;
for(int i=0;i!=len;i++)
{
if(str[i]==' ')
blackCnt++;
}
int newLen=len+2*blackCnt;
char* newStr=new char[newLen];
for(int i=len-1;i>=0;i--)
{
if(str[i]!=' ')
{
newStr[--newLen]=str[--len];
}else{
newStr[--newLen]='0';
newStr[--newLen]='2';
newStr[--newLen]='%';
len--;
}
}
return newStr;
}
int main(int argc, char const *argv[])
{
char* str1="I am ok";
char* str2="how you doing?";
cout<<replaceBlacnk(str1,7)<<endl;
cout<<replaceBlacnk(str2,14)<<endl;
return 0;
}