当前位置:网站首页>每日一题——替换空格
每日一题——替换空格
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语言——动态内存
感兴趣的朋友可以点击链接尝试用博主的方法,或者是自己的方法做做。
题目来源于:牛客网
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ
如有侵权请联系修改删除!
边栏推荐
- STM32 --- configuration of external interrupt
- Introduction of air gap, etc
- PMSM dead time compensation
- Carrier period, electrical speed, carrier period variation
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- Sword finger offer 05 Replace spaces
- STM32 single chip microcomputer - external interrupt
- General makefile (I) single C language compilation template
- Sql Server的存储过程详解
- [three tier architecture and JDBC summary]
猜你喜欢
Keil use details -- magic wand
Sword finger offer 06 Print linked list from end to end
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
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
Halcon's practice based on shape template matching [1]
Brief discussion on Buck buck circuit
Halcon's practice based on shape template matching [2]
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Example 009: pause output for one second
随机推荐
Nb-iot technical summary
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
动力电池UL2580测试项目包括哪些
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Relationship between line voltage and phase voltage, line current and phase current
Tailq of linked list
How to copy formatted notepad++ text?
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
【三层架构及JDBC总结】
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Step motor generates S-curve upper computer
Example 009: pause output for one second
The firmware of the connected j-link does not support the following memory access
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
剑指 Offer 09. 用两个栈实现队列
Negative pressure generation of buck-boost circuit
Management and use of DokuWiki (supplementary)
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Hardware and software solution of FPGA key chattering elimination
Wifi-802.11 negotiation rate table