zoukankan      html  css  js  c++  java
  • 剑指02替换空格

    题目描述

    请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

    # -*- coding:utf-8 -*-
    class Solution:
        # s 源字符串
        def replaceSpace(self, s):
            # write code here
            return "%20".join(list(s.split(" ")))

    public class Solution {
        public String replaceSpace(StringBuffer str) {
                StringBuffer out=new StringBuffer();
            for (int i=0;i<str.toString().length();i++){
                char b=str.charAt(i);
                if (String.valueOf(b).equals(" ")){
                    out.append("%20");
                }else {
                    out.append(b);
                }
            }
            return out.toString();
        }
    }

    class Solution {
    public:
        void replaceSpace(char *str,int length) {

            int  count=0;
            for (int i=0;i<length;i++){
                if (str[i]==' ')
                    count++;
            }
            for (int i=length-1;i>=0;i--){
                if (str[i]!=' ')
                {
                    str[i+2*count]=str[i];
                }
                else {
                    count--;
                    str[i+2*count]='%';
                    str[i+2*count+1]='2';
                    str[i+2*count+2]='0';
                }
            }
        }
    };

  • 相关阅读:
    Python面向对象
    Python函数
    Linux之路
    Python之路
    函数
    动态参数
    python模块的运行机制以及time模块格式转换
    Python PEP8代码规范_20180614
    Oracle 分页查询方法和效率分析
    oracle 12c数据库启动(包含CDB和PDB)以及常见异常处理
  • 原文地址:https://www.cnblogs.com/hrnn/p/13334439.html
Copyright © 2011-2022 走看看