把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
2018-02-14 14:21
344 查看
c++代码
#include<iostream>
using namespace std;
void replaceBlank(char *str)
{
if (str == NULL)
return;
int originalLength = 0, blank = 0, i = 0;
int newLength,originalIndex,newIndex;
while (str[i] != '\0')
{
originalLength++;
if (str[i] == ' ')
blank++;
i++;
}
newLength = originalLength + blank * 2;
originalIndex = originalLength;
newIndex = newLength;
while (originalIndex > 0 && newLength > originalIndex)
{
if (str[originalIndex] == ' ')
{
str[newIndex--] = '0';
str[newIndex--] = '2';
str[newIndex--] = '%';
}
else
str[newIndex--] = str[originalIndex];
originalIndex--;
}
}
int main()
{
char str[30] = "I love my hometown";
cout << "输入的字符串为" << endl;
cout << str<<endl;
replaceBlank(str);
cout << "替换空格后字符串为" << endl << str<<endl;
system("pause");
return 0;
}
运行结果
#include<iostream>
using namespace std;
void replaceBlank(char *str)
{
if (str == NULL)
return;
int originalLength = 0, blank = 0, i = 0;
int newLength,originalIndex,newIndex;
while (str[i] != '\0')
{
originalLength++;
if (str[i] == ' ')
blank++;
i++;
}
newLength = originalLength + blank * 2;
originalIndex = originalLength;
newIndex = newLength;
while (originalIndex > 0 && newLength > originalIndex)
{
if (str[originalIndex] == ' ')
{
str[newIndex--] = '0';
str[newIndex--] = '2';
str[newIndex--] = '%';
}
else
str[newIndex--] = str[originalIndex];
originalIndex--;
}
}
int main()
{
char str[30] = "I love my hometown";
cout << "输入的字符串为" << endl;
cout << str<<endl;
replaceBlank(str);
cout << "替换空格后字符串为" << endl << str<<endl;
system("pause");
return 0;
}
运行结果
相关文章推荐
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- 请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。
- 请实现一个函数,把字符串中的每个空格替换成“%20”,例如输入 “We are Happly。” 则输出“we%20are%20happy。”
- 【我的算法日记】请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。
- 请实现一个方法,把字符串中的每个空格替换成 %20 ,例如,输入"We are happy.",则输出"We%20are%20happy."
- 字符串替换空格: 请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we are happy.”。
- 字符串替换空格: 请实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we%20are%20happy.”。
- }1.字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we%20are%20happy.”。
- 字符串替换空格: 请实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we are happy.”
- [剑指offer][面试题04]实现一个函数,将字符串中的空格替换为“ ”.例如输入“we are happy.”,输出"we are happy.".
- 输入三个字符串,允许有空格,输出最大公共子串 例如输入: what your name my name is ABC