当前位置:网站首页>[C language] string reverse order implementation (recursion and iteration)
[C language] string reverse order implementation (recursion and iteration)
2022-07-28 20:03:00 【An ran_】
One 、 Thought analysis
1. Analysis of iteration method
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-yPBNzz4d-1651418567193)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220501224354336.png)]](/img/6f/e0ceb2e1a2c8903cfbb684206ce901.png)
2. Thinking analysis of recursive method ( Under very, very limited parameters )
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-egAHmHqx-1651418567195)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220501230958401.png)]](/img/c0/5710b3b02ddc3a37bbf0128522f252.png)
3. Analysis of optimized recursive method ( Sufficient parameters )
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-RjDZrB5F-1651418567196)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220501231017519.png)]](/img/c3/02d0a72f6026df8a67669293e55ef2.png)
Two 、 Code demonstration
// Reverse string order
#include <stdio.h>
#include <string.h>
// Method ① Circulation
void reverse1(char* arr,int sz)
{
int left = 0, right = sz - 2;
char t;
while (left <= right)
{
t = arr[left];
arr[left] = arr[right];
arr[right] = t;
left++;
right--;
}
}
// Method ② recursive ( With very few parameters , Allow library functions strlen)
void reverse2(char* str)
{
char tmp = *str;
int len = strlen(str);
*str = *(str + len - 1);
*(str + len - 1) = '\0';
if (strlen(str + 1) >= 2)
reverse2(str + 1);
*(str + len - 1) = tmp;
}
// Method ③ Recursive method ( Sufficient parameters )
void reverse3(char* arr, int left,int right)
{
char tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;// There is no need to zero the string
if(left<right)
reverse3(arr, left + 1, right - 1);
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcdef";
char arr3[] = "abcdef";
int sz1 = sizeof(arr1) / sizeof(arr1[0]);
int sz2 = sizeof(arr2) / sizeof(arr2[0]);
int sz3 = sizeof(arr3) / sizeof(arr3[0]);
reverse1(arr1, sz1);
reverse2(arr2);
reverse3(arr3, 0, sz3 - 2);
printf("%s\n", arr1);
printf("%s\n", arr2);
printf("%s\n", arr3);
return 0;
}
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-wjAhUD3i-1651418567197)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220501232133534.png)]](/img/49/d3b9b97ef6b3e3b476ead262148ebc.png)
边栏推荐
- MIR专题征稿 | 常识知识与推理:表示、获取与应用 (10月31日截稿)
- 爬取IP
- Cloud computing notes part.1 - system management
- Intermediate soft test (system integration project management engineer) high frequency test site
- Return and job management of saltstack
- Article translation software - batch free translation software supports major translation interfaces
- CDGA|工业互联网行业怎么做好数据治理?
- C language function
- Overcome the "fear of looking at teeth", and we use technology to change the industry
- 基于C语言的信息管理系统和小游戏
猜你喜欢

Edge detection and connection of image segmentation realized by MATLAB

How many types of rain do you know?

CDGA|工业互联网行业怎么做好数据治理?
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

基于MATLAB的函数拟合

What is the process of swing event processing?
![[C language] initial C language reflection and summary](/img/21/826d144867f7a73ec2cd8896a5250a.png)
[C language] initial C language reflection and summary

Tencent cloud deployment lamp_ Experience of building a station

Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
随机推荐
2022年下半年系统集成项目管理工程师认证8月20日开班
Basic concept and essence of Architecture
11. Learn MySQL union operator
Cloud computing notes part.1 - system management
Advanced notes (Part 2)
[C language] print pattern summary
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
爬取IP
[C language] initial C language reflection and summary
[network] communication across regional networks learn how routing tables work
leetcode day3 查找重复的电子邮箱
How to write the SQL statement of time to date?
Article translation software - batch free translation software supports major translation interfaces
CDGA|工业互联网行业怎么做好数据治理?
Cdga | how can the industrial Internet industry do a good job in data governance?
What parameters should be passed in calling integer or character array functions
Implementation of memcpy in C language
河北邯郸:拓展基层就业空间 助力高校毕业生就业
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Know small and medium LAN WLAN