当前位置:网站首页>每日一题——替换空格
每日一题——替换空格
2022-07-05 08:23:00 【保护小周ღ】
哈喽大家好,我是保护小周ღ,本期为大家带来的是牛客网上一道练习题——替换空格,博主分享两种解题思路(末尾附有本题链接),一起来看看把~
题目来源于牛客网
描述
请实现一个函数,将一个字符串s中的每个空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
数据范围:0 <=strlen(s) <=1000。保证字符串中的字符为大写英文字母、小写英文字母和空格中的一种。
示例1
输入:
"We Are Happy"返回值:
"We%20Are%20Happy"
示例2
输入:
" "返回值:
"%20"
思路解析:
题目主要信息:
- 将一个字符串 s 中的每个空格替换成“%20”;
- 保证字符串中的字符为大写英文字母、小写英文字母和空格中的一种;
定义一个字符数组ch[N],或者定义一个动态开辟的字符数组,我们可以采取遍历字符串 s 的方式确定空格的位置,将空格前的字符s的值一比一给到我们定义的字符数组ch,遇到空格时,将ch空格及以后的两个空间的值依次替换为'%','2','0'。直到 字符串 s 遇到结束标志‘\0’为止。
程序实现:
数组做法:
#include<stdio.h>
#include<string.h>
char* replaceSpace(char* s)
{
//因为数组是在栈区上面开辟的,函数结束栈帧销毁,所以要用static修饰数组,避免函数结束,数组销毁
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跟i保持同步
}
}
s = ch;
//根据题目要求返回指针类型
return s;
}
int main()
{
char arr[50] = {0};
gets(arr);
//替换空格
char *str=replaceSpace(arr);
printf("%s\n",str);
return 0;
}
动态开辟做法:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* replaceSpace(char* s)
{
// write code here
int N = strlen(s);//计算字符串长度
char tmp[] = { '%', '2', '0' };//替换内容
int space = 0;//计算空格数
for(int i=0;i<N;++i)
{
if (s[i]==' ')
{
++space;
}
}
int Len = N + space * 2;
//在堆区上动态开辟空间函数结束,不会主动销毁
char* str =(char*)malloc(Len*sizeof(char));
if (str == NULL)//如果动态开辟失败,结束程序
return;
//遍历字符串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);
//替换空格
char *str=replaceSpace(arr);
printf("%s\n",str);
return 0;
}
三个空格
本题注意点:
- 本题返回类型是char*
- 如果定义字符数组存储,该数组是在内存的栈区上开辟的,函数结束栈帧销毁,也就是开辟的这片空间被操作系统回收,此时,我们将这个数组地址作为返回值,就会造成非法访问内存,所以我们应当用 static修饰一下数组,让数组变成静态全局变量,就不会因为函数结束而销毁。
- 可用以malloc()在内存的堆区上开辟一块空间用于存储,在堆区上的空间除了我们主动free()释放和程序结束被操作系统回收以外,不会因为其他原因销毁。
如果大家对动态内存的相关知识有什么不懂的地方可以学习博主的另一篇博客:C语言——动态内存
感兴趣的朋友可以点击链接尝试用博主的方法,或者是自己的方法做做。
题目来源于:牛客网
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ
如有侵权请联系修改删除!
边栏推荐
- Shell script
- MHA High available Cluster for MySQL
- 实例009:暂停一秒输出
- Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
- Keil use details -- magic wand
- Live555 push RTSP audio and video stream summary (I) cross compilation
- Sizeof (function name) =?
- STM32 --- NVIC interrupt
- Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
- 实例010:给人看的时间
猜你喜欢
leetcode - 445. 两数相加 II
UE像素流,来颗“减肥药”吧!
Halcon's practice based on shape template matching [2]
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
STM32 single chip microcomputer - external interrupt
Working principle and type selection of common mode inductor
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Tailq of linked list
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
随机推荐
Several important parameters of LDO circuit design and type selection
实例007:copy 将一个列表的数据复制到另一个列表中。
Imx6ull bare metal development learning 1-assembly lit LED
Management and use of DokuWiki (supplementary)
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Stm32--- systick timer
Explain task scheduling based on Cortex-M3 in detail (Part 2)
DCDC circuit - function of bootstrap capacitor
OC and OD gate circuit
Solutions to compilation warnings in Quartus II
Negative pressure generation of buck-boost circuit
Problem solving: interpreter error: no file or directory
Classic application of MOS transistor circuit design (2) - switch circuit design
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Soem EtherCAT source code analysis I (data type definition)
Vofa+ software usage record
STM32 single chip microcomputer -- volatile keyword
Infected Tree(树形dp)
QEMU demo makefile analysis