当前位置:网站首页>[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)
边栏推荐
- MySQL命令语句(个人总结)
- C language operators and input and output
- Token verification program index.php when configuring wechat official account server
- 个人博克系统登录点击图形验证码的集成与实现
- 【微信小程序开发】页面导航与传参
- Codeignier framework implements restful API interface programming
- Article translation software - batch free translation software supports major translation interfaces
- 【NPP安装插件】
- Leetcode day2 连续出现的数字
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
猜你喜欢

2022年下半年系统集成项目管理工程师认证8月20日开班

Know small and medium LAN WLAN

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

How does app automated testing achieve H5 testing

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

党员故事|李青艾用漫画带动农民增收致富

冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

Netcoreapi operation excel table
随机推荐
[C language] summary of methods for solving the greatest common divisor
Theoretical knowledge of digital image (I) (personal analysis)
Cell review: single cell methods in human microbiome research
Sequential linear table - practice in class
Array method added in ES6
MATLAB实现的图像分割之边缘检测和连接
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
Return and job management of saltstack
Token verification program index.php when configuring wechat official account server
Basic concept and essence of Architecture
Leetcode Day1 score ranking
Implementation of memcpy in C language
Idea properties file display \u solution of not displaying Chinese
[NPP installation plug-in]
Leetcode day3 find duplicate email addresses
Redis笔记
This customized keyboard turns me on~
2022年下半年系统集成项目管理工程师认证8月20日开班
[C language] initial C language reflection and summary
Two methods to judge the size end