首页 > 其他 > 详细

careercup-1.4

时间:2014-12-03 17:01:56      阅读:246      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!