当前位置:网站首页>C language replaces spaces in strings with%20
C language replaces spaces in strings with%20
2022-07-02 08:28:00 【Ischanged】
subject : Please implement a function , Replace each space in the string with "%20". Examples : “abc defgx yz” turn “abc%20defgx%20yz”
First time to see this topic , I wrote the following error code , First of all, let's think about what's wrong ?
#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);// The result is :abc 躣躣躣躣躣躣躣躣躣躣躣 efgx 躽 z
return 0;
}
In fact, the simple idea of the above code is through pointers , Traverse this character array , If you find a space, replace it with %20, Always find the end of the string \0 Up to . But for this problem, I have neglected an important problem , Is that we find the space and replace it with %20, A space is a character ,%20 It's three characters , Replace three characters with one character , How can memory space be filled , Causes the array to be out of bounds , As for the result, it is also strange .
An array can't be troublesome , To find a space, you need to move the following element , Then we can define two arrays
#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";;// The array size is 13
char arr2[40];// Arbitrarily define the appropriate array size , I can put it down arr1 The content of
replace(arr1, arr2);
printf("%s", arr2);
return 0;
}
Program run results :
The basic implementation idea of the above code is :
- Define two arrays ,arr1 Used to store the original string ,arr2 Used to store added %20 String after
- Traverse the array through the pointer arr1, Look for spaces , Until you find the string \0 until , If it is not a space during traversal , Character by character arr1 Put the content of arr2 Inside , If you encounter a space, put it after the character that is not a space , hold %20 Copy to the end of the string ,arr2 The space in front is full , So the position of the next character should be moved back three positions . Continue to implement this Law .
- Finally, when you find arr1 Of \0 Replacement complete , stay arr2 After the replaced character, add \0 that will do ( because arr2 Only defined but not initialized , There's nothing in it )
Another algorithm is also ok , Look at the code first :
#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;// Three positions have been occupied ,p Move back three positions
continue;//p Already moved , So don't move , Use continue Skip the following code p++
}
p++;
}
return str;
}
int main()
{
char arr1[30] = "abc defgx yz";//14
//char arr2[40];
replace(arr1);
printf("%s", replace(arr1));
return 0;
}
This algorithm idea is a little similar to the above , The code above is added to the second array %20 after , Output this array arr2, Do not use the original array ; The following idea is to borrow an array to temporarily store things , I added %20 after , Then put the things temporarily stored behind the blank , Get back the original array .
The basic idea of the above code is : Every time you need to save the string after the space into an array , Then replace the space with %20 Then copy the string just copied to %20 You can just .
边栏推荐
- The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
- How to apply for a secondary domain name?
- 乐理基础(简述)
- Carla-ue4editor import Roadrunner map file (nanny level tutorial)
- Erase method in string
- Pointer initialization
- HCIA - application layer
- Generate database documents with one click, which can be called swagger in the database industry
- On November 24, we celebrate the "full moon"
- install. IMG production method
猜你喜欢

Carla-UE4Editor导入RoadRunner地图文件(保姆级教程)

2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers

c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)

sqli-labs第12关

Introduction to parameters of CarSim pavement 3D shape file

When a custom exception encounters reflection

On November 24, we celebrate the "full moon"

【无标题】

File upload and download performance test based on the locust framework

ICMP协议
随机推荐
What are the platforms for selling green label domain names? What is the green label domain name like?
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Wang extracurricular words
Global and Chinese market of medicine cabinet 2022-2028: Research Report on technology, participants, trends, market size and share
Carla-ue4editor import Roadrunner map file (nanny level tutorial)
ARP and ARP Spoofing
类和对象(类和类的实例化,this,static关键字,封装)
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
力扣每日一题刷题总结:字符串篇(持续更新)
Valin cable: BI application promotes enterprise digital transformation
ICMP协议
16: 00 interview, came out at 16:08, the question is really too
Sqlyog remote connection to MySQL database under centos7 system
实现双向链表(带傀儡节点)
Carsim-实时仿真的动画同步问题
How to build the alliance chain? How much is the development of the alliance chain
路由基础—动态路由
旋转链表(图解说明)
文件上传-upload-labs
Mutex