-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-replaceSpace.cpp
executable file
·61 lines (55 loc) · 1.15 KB
/
03-replaceSpace.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/********************************
*@file:
*@author: Pan HU
*@data:
*@version: 0.1
*@describe:
********************************/
#include <iostream>
#include <cstring>
using namespace std;
void replaceSpace(char *src, int len)
{
if(src == NULL || len <= 0)
{
return;
}
int iSpaceNum(0),iSrcLen(0);
for(int i = 0; src[i] != '\0'; ++i)
{
++iSrcLen;
if(*(src+i) == ' ')
{
++iSpaceNum;
}
}
int iNewLen = iSrcLen + iSpaceNum*2;
cout<<iSpaceNum<<" "<<iSrcLen<<" "<<iNewLen<<endl;
if(iNewLen >= len)
{
return;
}
for(int i = iSrcLen-1; i >= 0; --i)
{
if(src[i] == ' ')
{
src[-1+iNewLen--] = '0';
src[-1+iNewLen--] = '2';
src[-1+iNewLen--] = '%';
}
else
{
src[-1+iNewLen--] = src[i];
}
}
src[iSrcLen + iSpaceNum*2] = '\0';
cout<<src<<endl;
}
int main1()
{
char arr[100];
memset(arr,0,sizeof(char)*100);
strcpy(arr,"hello world!");
replaceSpace(arr,100);
return 0;
}