当前位置:网站首页>c语言将字符串中的空格替换成%20
c语言将字符串中的空格替换成%20
2022-07-02 06:28:00 【Ischanged】
题目:请实现一个函数,把字符串中的每个空格替换成"%20"。样例: “abc defgx yz” 转“abc%20defgx%20yz”
第一次看到这道题目,我写了一个如下的错误代码,大家首先思考下哪里错了呢?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<assert.h>
char *replace(char *p)
{
assert(p != NULL);
char *s = p;
while (*p)
{
if (*p == ' ')
*p = "%20";
p++;
}
return s;
}
int main()
{
char a[] = "abc defgx yz";
replace(a);
printf("%s", a);//结果为:abc躣efgx躽z
return 0;
}
其实上面代码的简单思想就是通过指针,遍历这个字符数组,找到空格就把它替换为%20,一直找到字符串末尾\0的位置为止。但是对于这一题我忽略了一个重要的问题,就是我们找到空格把它替换为%20,空格是一个字符,%20是三个字符,把三个字符替换为一个字符,内存空间怎么塞得下,造成数组越界,也至于结果也是奇奇怪怪的。
一个数组不行麻烦,找到空格需要移动后面的元素,那我们定义两个数组即可
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
void replace(char *arr1, char *arr2)
{
while (*arr1)
{
if (*arr1 != ' ')
{
*arr2 = *arr1;
arr2++;
}
else
{
strcpy(arr2, "%20");
arr2 = arr2 + 3;
}
arr1++;
}
*arr2 = '\0';
}
int main()
{
char arr1[] = "abc defgx yz";;//数组大小为13
char arr2[40];//随意定义合适的数组大小,放的下arr1的内容即可
replace(arr1, arr2);
printf("%s", arr2);
return 0;
}
程序运行结果:
上面代码的基本实现思路为:
- 定义两个数组,arr1用来存原字符串,arr2用来存放添加了%20后的字符串
- 通过指针遍历数组arr1,寻找空格,直到找到字符串的\0为止,如果在遍历过程中不是空格,一个字符一个字符的把arr1的内容放到arr2里面,如果遇到空格在放完不是空格的字符后面,把%20拷贝到放完后字符串的后面,arr2前面的空间已经占满,所以下一次放字符的位置要向后移动三个位置。以此规律继续执行下去。
- 最后当找到arr1的\0替换完成,在arr2替换完的字符后面加上\0即可(因为arr2只定义没有初始化,里面没有内容)
还有另外一种算法也行,先看代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
char * replace (char *str)
{
char *p = str;
char arr[30];
while (*p != '\0')
{
if (*p == ' ')
{
strcpy(arr, p + 1);
*p = '%';
*(p + 1) = '2';
*(p + 2) = '0';
strcpy(p + 3, arr);
p = p + 3;//有三个位置已经被占用了,p向后移动三个位置
continue;//p已经移动过了,所以不要移动了,使用continue跳过后面的代码p++
}
p++;
}
return str;
}
int main()
{
char arr1[30] = "abc defgx yz";//14
//char arr2[40];
replace(arr1);
printf("%s", replace(arr1));
return 0;
}
这种算法思想和上面的有点相似,上面那个代码是在第二个数组里面加了%20后,输出这个数组arr2,不使用原来的数组;下面的这种思想是借用一个数组临时存放东西,在添加了%20后,再把空格后面临时存放的东西,拿回原来的数组。
上面代码的基本思路是: 每次都需要把空格后的字符串保存到一个数组中,然后把空格替换为%20后再将刚刚拷贝的字符串拷贝到%20的后面即可。
边栏推荐
- 力扣每日一题刷题总结:字符串篇(持续更新)
- Find and rfind methods in string
- 程序猿学英语-Learning C
- Short video with goods source code, double-click to zoom in when watching the video
- Using C language to realize MySQL true paging
- STL速查手册
- Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
- Meta learning Brief
- Erase method in string
- 关于原型图的深入理解
猜你喜欢

2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers

On November 24, we celebrate the "full moon"

Fundamentals of music theory (brief introduction)

Vs code configuration problem

Sequence problem for tqdm and print

Use the kaggle training model and download your own training model

类和对象(类和类的实例化,this,static关键字,封装)

Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?

On the confrontation samples and their generation methods in deep learning

Dynamic extensible representation for category incremental learning -- der
随机推荐
学习写文章格式
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
深入理解JVM
11月24号,我们为“满月”庆祝
Target detection for long tail distribution -- balanced group softmax
OpenCV3 6.3 用滤波器进行缩减像素采样
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
Carsim 学习心得-粗略翻译1
Carsim-路面3D形状文件参数介绍
Use the kaggle training model and download your own training model
Opencv3 6.3 reduced pixel sampling with filters
High school mathematics compulsory one
力扣方法总结:滑动窗口
It's great to save 10000 pictures of girls
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
Principes fondamentaux de la théorie musicale (brève introduction)
Installation and use of simple packaging tools
关于原型图的深入理解
OpenCV3 6.2 低通滤波器的使用