2空字符串替换
来源:互联网 发布:小白自学java看什么书 编辑:程序博客网 时间:2024/05/17 22:58
#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
/*****************************************************************************
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
总结:这个题目是有点问题的,就是字符空间变大后,申请空间的问题,这个你怎么搞,
使用malloc就可以使用变量做参数进行空间分配不能使用变量(虽然值是常数字),然
后对空间末尾添加结束标志这个题目是有点意思的,我现在才开始做题
******************************************************************************/
class solution{
public:
void replace(char*str, int length){
for (char*p = str; *p != '\0'; p++)
cout << *p;
cout << endl;
if (length <= 0 || str == NULL)
return;
int i = 0, spacenum = 0;
while (str[i] != '\0'){
if (str[i] == ' '){
spacenum++;//这里是统计多少个空字符
}
i++;//这里是统计有多少个字符,包括空字符
}
int oldlen = i;
int newlen = oldlen + spacenum * 2;
char *p = (char*)malloc(sizeof(char)*newlen);
for (i = oldlen; i >= 0; i--){
if (str[i] == ' '){
p[newlen--] = '0';
p[newlen--] = '2';
p[newlen--] = '%';
}
else{
p[newlen--] = str[i];
}
}
p[newlen] = '\0';
for (char*p1 = p; *p1 != '\0'; p1++)
cout << *p1;
cout << endl;
}
};
void main(){
char p[] = "we are happy";
solution bb;
bb.replace(p,12);
system("pause");
}
#include<vector>
#include<string>
#include<stack>
using namespace std;
/*****************************************************************************
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
总结:这个题目是有点问题的,就是字符空间变大后,申请空间的问题,这个你怎么搞,
使用malloc就可以使用变量做参数进行空间分配不能使用变量(虽然值是常数字),然
后对空间末尾添加结束标志这个题目是有点意思的,我现在才开始做题
******************************************************************************/
class solution{
public:
void replace(char*str, int length){
for (char*p = str; *p != '\0'; p++)
cout << *p;
cout << endl;
if (length <= 0 || str == NULL)
return;
int i = 0, spacenum = 0;
while (str[i] != '\0'){
if (str[i] == ' '){
spacenum++;//这里是统计多少个空字符
}
i++;//这里是统计有多少个字符,包括空字符
}
int oldlen = i;
int newlen = oldlen + spacenum * 2;
char *p = (char*)malloc(sizeof(char)*newlen);
for (i = oldlen; i >= 0; i--){
if (str[i] == ' '){
p[newlen--] = '0';
p[newlen--] = '2';
p[newlen--] = '%';
}
else{
p[newlen--] = str[i];
}
}
p[newlen] = '\0';
for (char*p1 = p; *p1 != '\0'; p1++)
cout << *p1;
cout << endl;
}
};
void main(){
char p[] = "we are happy";
solution bb;
bb.replace(p,12);
system("pause");
}
0 0
- 2空字符串替换
- 剑指offer 2 替换空字符串
- 字符串空值替换
- 正则替换字符串为空
- Gson解析null替换为空字符串
- Gson解析null替换为空字符串
- Gson解析null替换为空字符串
- Gson解析null替换为空字符串
- Gson解析null替换为空字符串
- 用空字符串替换除数字以外的字符
- mysql替换字符串中的最后一个“-”为空
- 难度2 字符串替换
- js的replace方法将字符串中的 全部替换为空字符串
- 将字符串中的空字符全部替换为别的字符串 Python 版
- 字符串替换
- 字符串替换
- 替换字符串
- 字符串替换
- EhCache缓存框架简介
- Linux初级学习第十一单元
- Intel SGX技术学习了解
- mysql删除有空格字符名称的触发器
- 欢迎使用CSDN-markdown编辑器
- 2空字符串替换
- PAT_乙级_1013
- 题目1013:开门人和关门人
- HDU 2010 水仙花数 水题 解法
- 数据库使用cmd打开
- SOPHP微信公众号免费开源推广
- 第一个版本库 Repository
- Market-Oriented Cloud Computing文章阅读笔记
- Git操作之克隆某一个特定的远程分支