题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
i=0
n=len(s)
ss=[]##初始化定义算法的中间变量
for i in range(n):
if s[i].isspace():
ss.append('%20')#判断为空的时候加入非空的时候不加入
else:
ss.append(s[i])
i+=1
ss=''.join(ss)##把所有的都连在一起来
return ss

  

c++代码

#include<iostream>
using namespace std;
class Solution {
public:
void replaceSpace(char *str, int length) {
if (str == nullptr || length<0)
return;
int count = 0, i = 0;
while (str[i] != '\0')
{
if (str[i] == ' ')
{
count++;
}
i++;
}
int oldlen = i;
int newlen = i + count * 2;
if (newlen>length)
return;
while (oldlen != newlen)
{
if (str[oldlen] != ' ') {
str[newlen--] = str[oldlen];
}
else {
str[newlen--] = '0';
str[newlen--] = '2';
str[newlen--] = '%';
}
oldlen--;
}
return;
} };
int main(){
const int length = 100;
char str[length] = "We Are Happy";
Solution().replaceSpace(str, length);
cout << str << endl;
system("pause");
return 0;
}

  

替换空格(python)的更多相关文章

  1. 替换空格[by Python]

    题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 1.使用python自带的repla ...

  2. 替换空格(C++和Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 请实现一个函数,把字符串中的每个空格替换为 "%2 ...

  3. 《剑指offer》替换空格

    本题来自<剑指offer> 替换空格 题目: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are% ...

  4. LeetCode替换空格

    LeetCode 替换空格 题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 实例 1: 输入:s = "We are happy." 输 ...

  5. 剑指Offer面试题:3.替换空格

    一.题目:替换空格 题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 在网络编程中 ...

  6. 剑指Offer 替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.   思路: 替换空格,先遍历一遍记 ...

  7. 剑指Offer:面试题4——替换空格(java实现)

    问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...

  8. 【面试题004】c/c++字符串,替换空格

      一,c/c++字符串 1.C/C++中每个字符串都以字符’\0‘作为结尾,这样我们就能很方便地找到字符串的最后尾部. 由于这个原因每个字符串都有一个额外的开销,注意字符串越界的问题: 2.C/C+ ...

  9. 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

    //字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...

随机推荐

  1. Android 开发第三步-问题解析

    因为这个问题,困扰了好久 最后找到了最根本的问题,当时是因为没有R.java为什么没有? 是因为布局出现了错误,在一篇百度经验中找到了怎样解决 https://jingyan.baidu.com/ar ...

  2. leetcode994

    public class Solution { ; ; ; ; ; Queue<int[]> Q = new Queue<int[]>(); int[,] TagGrid; p ...

  3. XML中的变量传值

    在action的java类中定义变量之后,在XML中获取该变量进行对应传值:: 在指定方法中获取XML配置文件的变量传值::

  4. AJax知识介绍

    参考:http://www.runoob.com/ajax/ajax-asp-php.html

  5. 修改 计算机名后,修改SQLserver 注册服务器对象的名称,及登陆名

    select @@ServerName --查看当前所有数据库服务器名称select * from Sys.SysServers --修改数据库服务器名称sp_dropserver 'old_serv ...

  6. C#可变参数params

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestPara ...

  7. English Phrases with THE – Linking the TH Sound

    English Phrases with THE – Linking the TH Sound Share Tweet Share Tagged With: The Word THE Study En ...

  8. Haskell语言学习笔记(89)Unicode UTF8

    unicode-show $ cabal install unicode-show Installed unicode-show-0.1.0.2 Prelude> :m +Text.Show.U ...

  9. [Shell]一张图知道Shell(图)

  10. 判断页面是否添加了W3C声明

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...