编程(用%20替换空格)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程(用%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

 

以上是关于编程(用%20替换空格)的主要内容,如果未能解决你的问题,请参考以下文章

编写一个方法,用 '%20' 替换字符串中的所有空格

替换空格

面试题5:替换空格

面试题5:替换空格

剑指offer字符串面试题 替换空格

剑指offer字符串面试题 替换空格