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';
                }
            }
        }
    };

  • 相关阅读:
    springboot整合极光推送实现APP通知
    微信服务商AppID账号与商户号配置和查看
    linux-contos7中安装nginx
    centos 7 安装sql server 2017
    Redis集群高可用
    检查预约业务系统交互时序图
    特殊字符检测
    for循环执行原理
    Web.py报错:OSError: No socket could be created -- (('0.0.0.0', 8080):
    Oracle导出数据中的prompt,set feedback 等是什么意思
  • 原文地址:https://www.cnblogs.com/hrnn/p/13334439.html
Copyright © 2011-2022 走看看