当前位置:网站首页>【C】 Reverse string (two recursive ideas)
【C】 Reverse string (two recursive ideas)
2022-07-29 00:00:00 【Xin-Xiang Rong】
Blog home page : XIN-XIANG Rong
Series column :【 from 0 To 1,C Language learning 】
A short sentence : If you are in full bloom , Butterflies come !
Blog description : Do what you can , Write every blog , Help yourself familiarize yourself with what you have learned , I also hope these contents can help some partners on the way of learning , If errors and deficiencies are found in the article , Also hope to leave a message in the comment area , We communicate progress together !
List of articles
Preface
Very simple reverse string implementation , Record a recursive idea that is not easy to think of !
Fulfill the requirements :
Invert the characters in the parameter string , Not in reverse order .
such as :
char arr[ ] = “abcdef”;
In reverse order, the contents of the array become :fedcba
Ideas 1:
Grasp the core idea of recursion and make the event small , To reverse the entire string , You can first exchange the first and last characters in the string , Then reverse the middle string ;
And the middle string can be recursive again with the above idea , Until there is no string in the middle of the first and last characters ;

If you want to recurse the middle content as a string , You need to put one behind it \0, And because you want to put this \0, The first character cannot be placed at the end of the string to complete the exchange , At this point, you can store this character with an intermediate variable , Wait until the middle string recursion completes the reverse order , Then put the stored first character in the position of the last character .

#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse(char* str)
{
assert(str);
char tmp = *str;
int len = strlen(str);
*str = *(str + len - 1);
*(str+len - 1) = '\0';
if (my_strlen(str + 1) >= 2)
{
reverse(str + 1);
}
*(str + len - 1) = tmp;
}
int main()
{
char arr[] = "abcdefg";
reverse(arr);
printf("%s\n", arr);
return 0;
}
Ideas 2:
Implement a recursive function to exchange strings two by two .

#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse(char arr[], int left, int right)
{
assert(arr);
if (left < right)
{
char tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
reverse(arr, left + 1, right - 1);
}
}
int main()
{
char arr[] = "abcdefg";
int left = 0;
int right = strlen(arr)-1;
reverse(arr, left, right);
printf("%s\n", arr);
return 0;
}
Conclusion
Dear friends , It's fate to see here , I hope my content can bring you a little help , If you can, support it for three times !!! Thank you for coming here , We can learn and communicate together , Progress together !!! come on. !!!

边栏推荐
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
- Worthington核糖核酸酶B历史和化学性质说明
- 器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
- Zabbix 5.0 使用自带Redis模版监控
- 1-6 state与绑定事件
- Leetcode63. 不同路径 II
- Working principle of fastdfs (technical principle)
- 【微服务】Nacos集群搭建以及加载文件配置
- Leetcode62. 不同路径
- Worthington RNA determination detailed introduction
猜你喜欢

Explanation of history and chemical properties of Worthington ribonuclease B

Deep analysis of integrated learning AdaBoost

Multisensor fusion positioning (III) -- inertial technology

pycharm新建项目

Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan

Multi sensor fusion positioning (II) -- map based positioning

实时数仓:网易严选基于Flink的实时数仓实践

失败率高达80%,数字化转型如何正确完成战略规划?

RHCE the next day

基于 FPGA 实现数字时钟详细原理讲解及验证结果
随机推荐
JS高级 之 ES6~ES13 新特性
Worthington丨Worthington胰蛋白酶化学性质及相关研究
Working principle of fastdfs (technical principle)
Use pytoch to quickly train the network model
Multisensor fusion positioning (III) -- inertial technology
Pycharm new project
Yolov5 learning notes (I) -- principle overview
Arm-A53资料「建议收藏」
Leetcode64. 最小路径和
C语言 n*n矩阵求值及求逆矩阵[通俗易懂]
软件设计师的错题汇总
With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally
Multi sensor fusion positioning (II) -- map based positioning
Deep analysis of integrated learning AdaBoost
Powercli batch add esxi to vCenter
Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
What is a driver signature and how does the driver get a digital signature?
Doip test development practice
Js判断数据类型的4种⽅式
Leetcode 763. partition labels divide alphabetic intervals (medium)
