当前位置:网站首页>One question per day - replace spaces
One question per day - replace spaces
2022-07-05 08:28:00 【Protect Xiaozhou】
Hello, everyone , I'm protecting Xiao Zhou ღ, This issue brings you an exercise on Niuke online —— Replace blank space , Bloggers share two ways to solve problems ( A link to this question is attached at the end ), Let's have a look ~
The topic comes from niuke.com
describe
Please implement a function , Put a string s Replace each space in with “%20”.
for example , When the string is We Are Happy. The replaced string is We%20Are%20Happy.
Data range :0 <=strlen(s) <=1000. Ensure that the characters in the string are capitalized English letters 、 One of lowercase letters and spaces .
Example 1
Input :
"We Are Happy"Return value :
"We%20Are%20Happy"
Example 2
Input :
" "Return value :
"%20"
Thinking analysis :
Title main information :
- Put a string s Replace each space in with “%20”;
- Ensure that the characters in the string are capitalized English letters 、 One of lowercase letters and spaces ;
Defines an array of characters ch[N], Or define a dynamically opened character array , We can take traversal strings s To determine the position of the space , Put the character before the space s The value of one to one is given to the character array we define ch, When spaces are encountered , take ch The values of spaces and the next two spaces are replaced by '%','2','0'. until character string s End flag encountered ‘\0’ until .
Program realization :
Array approach :
#include<stdio.h>
#include<string.h>
char* replaceSpace(char* s)
{
// Because the array is opened on the stack area , Function end stack frame destroy , So want to use static Modify the array , Avoid function end , Array destroy
static char ch[50] = { 0 };
for (int i = 0,j = 0; s[i] != '\0'; i++, j++)
{
if (s[i] != ' ')
{
ch[j] = s[i];
}
else if (s[i] == ' ')
{
ch[j] = '%';
ch[j + 1] = '2';
ch[j + 2] = '0';
j += 2;//j Follow i Keep in sync
}
}
s = ch;
// Return the pointer type according to the requirements of the topic
return s;
}
int main()
{
char arr[50] = {0};
gets(arr);
// Replace blank space
char *str=replaceSpace(arr);
printf("%s\n",str);
return 0;
}
Dynamic development practices :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* replaceSpace(char* s)
{
// write code here
int N = strlen(s);// Calculate string length
char tmp[] = { '%', '2', '0' };// replace content
int space = 0;// Calculate the number of spaces
for(int i=0;i<N;++i)
{
if (s[i]==' ')
{
++space;
}
}
int Len = N + space * 2;
// The function of dynamically opening up space on the heap ends , Will not actively destroy
char* str =(char*)malloc(Len*sizeof(char));
if (str == NULL)// If dynamic development fails , End procedure
return;
// Traversal string s
int i = 0;
while (*s)
{
if (*s == ' ')
{
int n = 0;
while (n < 3)
{
str[i] = tmp[n];
++i;
++n;
}
}
else
{
str[i] = *s;
++i;
}
++s;
}
return str;
}
int main()
{
char arr[50] = {0};
gets(arr);
// Replace blank space
char *str=replaceSpace(arr);
printf("%s\n",str);
return 0;
}
Three spaces
Pay attention to :
- The return type of this question is char*
- If you define character array storage , The array is opened on the stack area of memory , Function end stack frame destroy , That is, the space opened up is recycled by the operating system , here , We take this array address as the return value , It will cause illegal access to memory , So we should use static Decorate the array , Let the array become a static global variable , It will not be destroyed because the function ends .
- Can be used to malloc() Open up a space on the heap of memory for storage , In addition to our initiative in the space on the heap free() Release and program end are recycled by the operating system , It will not be destroyed for other reasons .
If you don't know anything about dynamic memory, you can learn another blog of the blogger :C Language —— Dynamic memory
Interested friends can click on the link to try to use the blogger's method , Or do it in your own way .
The title comes from : Cattle from
link : Replace blank space _ Niuke Tiba _ Cattle from
Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ
If there is infringement, please contact to modify and delete !
边栏推荐
- Stm32--- systick timer
- Bootloader implementation of PIC MCU
- 【三层架构及JDBC总结】
- H264 (I) i/p/b frame gop/idr/ and other parameters
- STM32 virtualization environment of QEMU
- General makefile (I) single C language compilation template
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- 实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
- Sword finger offer 05 Replace spaces
- Sword finger offer 06 Print linked list from end to end
猜你喜欢
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Hardware 3 -- function of voltage follower
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Example 006: Fibonacci series
Sword finger offer 06 Print linked list from end to end
实例007:copy 将一个列表的数据复制到另一个列表中。
C language # and #
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
随机推荐
Nb-iot technical summary
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Negative pressure generation of buck-boost circuit
Management and use of DokuWiki
Example 008: 99 multiplication table
Example 010: time to show
MySQL之MHA高可用集群
实例007:copy 将一个列表的数据复制到另一个列表中。
Infected Tree(树形dp)
Circleq of linked list
UE像素流,来颗“减肥药”吧!
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
matlab timeserise
如何写Cover Letter?
Weidongshan Internet of things learning lesson 1
Semiconductor devices (I) PN junction
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Example 009: pause output for one second
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Bootloader implementation of PIC MCU