当前位置:网站首页>[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)
边栏推荐
- 数字图像理论知识(一)(个人浅析)
- 11. Learn MySQL union operator
- Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
- 基于C语言的信息管理系统和小游戏
- Redis笔记
- Leetcode day4 the highest paid employee in the Department
- 利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
- Basic knowledge of C language
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
猜你喜欢

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

冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题

Function fitting based on MATLAB

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

【NPP安装插件】

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

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

String中常用的API
![[C language] print pattern summary](/img/48/d8ff17453e810fcd9269f56eda4d47.png)
[C language] print pattern summary

This customized keyboard turns me on~
随机推荐
C language pointer and two-dimensional array
Codeignier framework implements restful API interface programming
What parameters should be passed in calling integer or character array functions
Two methods to judge the size end
Why is there no log output in the telnet login interface?
Edge detection and connection of image segmentation realized by MATLAB
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
Cloud computing notes part.2 - Application Management
Design of air combat game based on qtgui image interface
In the second half of 2022, the system integration project management engineer certification starts on August 20
Use of strtok and strError
The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
Can China make a breakthrough in the future development of the meta universe and occupy the highland?
Array method added in ES6
11. Learn MySQL union operator
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
基于MATLAB的函数拟合
Servlet learning notes