编程(用%20替换空格)

题目描述

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     void replaceSpace(char *str,int length);
 8 };
 9 
10 void Solution::replaceSpace(char *str, int length)
11 {
12     vector<char> str1;
13     for(int i = 0; i < length; i++)
14     {
15         if(str[i] != ' ')
16         {
17             str1.push_back(str[i]);
18         }
19 
20         else
21         {
22             str1.push_back('%');
23             str1.push_back('2');
24             str1.push_back('0');
25         }
26     }
27 
28     for(int t = str1.size()-1; t >= 0; t--)
29     {
30         str[t] = str1[t];
31     }
32 }
View Code

 

posted on 2017-04-06 15:06  xdzhanght  阅读(154)  评论(0编辑  收藏  举报

导航