码迷,mamicode.com
首页 > 其他好文 > 详细

careercup-1.4

时间:2014-12-03 17:04:37      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   div   

1.4 编写一个方法,将字符串中的空格全部替换为“%20“。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的”真实“长度。

C++实现代码:

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string replacespace(string str)
{
    if(str.empty())
        return "";
    int i;
    int len=str.length();
    string res;
    for(i=0;i<len;i++)
    {
        if(isspace(str[i]))
            res+="%20";
        else
            res+=str[i];
    }
    return res;
}

int main()
{
    string str="We are Happy";
    cout<<replacespace(str)<<endl;
}

 

careercup-1.4

标签:style   blog   io   ar   color   os   sp   for   div   

原文地址:http://www.cnblogs.com/wuchanming/p/4140372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!