替换字符串中空格为%20
2017-01-08 21:00
375 查看
/*
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
/*
方法一:
创建足够长的新存储空间
*/
void replaceSpace_1(char* str)
{
char* str1 = (char*)malloc(100);
char *s = str1;
//注意不要直接移动原来的指针,如果直接移动原来的指针,那么最后这个指针就不指向原来创建的地址头,导致输出失败 ;
if(s == NULL ||str == NULL)
return;
while(*str != '\0')
{
//if(*str++ == ' ') 这个是错的 如果判断不是空 这个时候也会使指针移动 导致遗漏 因为这句执行完之后,str就指向下一位了
if(*str == ' ')
{
*s++ = '%';
*s++ = '2';
*s++ = '0';
}
else
{
*s++ = *str;
}
str++;
}
*s = '\0';
puts(str1);
free(str1);
}
/*
方法二:
假设原来输入的字符串长度很长,在原输入的基础上直接操作 注意要从后往前插入,可以减少移位的次数
*/
void replaceSpace_2(char* str,int length){
if(str == NULL || length <= 0)
return;
char *s = str;
int lenofstr = 1;
int numofblank = 0;
while(*s != '\0'){
lenofstr++;
if(*s == ' ')
numofblank++;
s++;
}
int newlength = lenofstr + 2*numofblank;
if(newlength > length)
return;
int index1 = lenofstr - 1;
int index2 = newlength - 1;
while(index1 >= 0 && index2 > index1){
if(str[index1] == ' ')
{
str[index2--] = '0';
str[index2--] = '2';
str[index2--] = '%';
}
else
{
str[index2--] = str[index1];
}
index1--;
}
cout << str << endl;
}
int main()
{
char s[100] = "we are happy new year.";
//replaceSpace_1(s);
replaceSpace_2(s,100);
return 0;
}
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
/*
方法一:
创建足够长的新存储空间
*/
void replaceSpace_1(char* str)
{
char* str1 = (char*)malloc(100);
char *s = str1;
//注意不要直接移动原来的指针,如果直接移动原来的指针,那么最后这个指针就不指向原来创建的地址头,导致输出失败 ;
if(s == NULL ||str == NULL)
return;
while(*str != '\0')
{
//if(*str++ == ' ') 这个是错的 如果判断不是空 这个时候也会使指针移动 导致遗漏 因为这句执行完之后,str就指向下一位了
if(*str == ' ')
{
*s++ = '%';
*s++ = '2';
*s++ = '0';
}
else
{
*s++ = *str;
}
str++;
}
*s = '\0';
puts(str1);
free(str1);
}
/*
方法二:
假设原来输入的字符串长度很长,在原输入的基础上直接操作 注意要从后往前插入,可以减少移位的次数
*/
void replaceSpace_2(char* str,int length){
if(str == NULL || length <= 0)
return;
char *s = str;
int lenofstr = 1;
int numofblank = 0;
while(*s != '\0'){
lenofstr++;
if(*s == ' ')
numofblank++;
s++;
}
int newlength = lenofstr + 2*numofblank;
if(newlength > length)
return;
int index1 = lenofstr - 1;
int index2 = newlength - 1;
while(index1 >= 0 && index2 > index1){
if(str[index1] == ' ')
{
str[index2--] = '0';
str[index2--] = '2';
str[index2--] = '%';
}
else
{
str[index2--] = str[index1];
}
index1--;
}
cout << str << endl;
}
int main()
{
char s[100] = "we are happy new year.";
//replaceSpace_1(s);
replaceSpace_2(s,100);
return 0;
}
相关文章推荐
- 设计模式—原型模式
- 阅读《effective java-第17条》遇到的问题解决与分享
- java web小结(二)
- c#复制图片到粘贴板
- Node调试工具JSHint
- 419. Battleships in a Board 难度:medium
- HTTPS和HTTP的区别
- 也谈10000小时
- c语言----函数的递归
- Markdown使用
- 从源码角度分析一下OnMeasure
- HTML学习笔记(1)
- openjdk8的安装
- http学习笔记(模拟http请求和响应过程)
- PAT BASIC LEVEL 1005. 继续(3n+1)猜想 (25)
- Leetcode 136. Single Number JAVA语言
- Java学习篇之IO流(字节流)
- 不一样的S型方阵
- 02http协议及httpd基础
- CentOS6.5_64位系统下安装配置postfix邮件系统 启用并配置SMTP在第三方上边使用发送邮件