当前位置:网站首页>Two methods of printing strings in reverse order in C language
Two methods of printing strings in reverse order in C language
2022-07-25 22:31:00 【Gao You Wu Shao】
Non recursive
Non recursive thinking is more clever , Here's the picture :
Let's exchange the first one with the penultimate one , Exchange the second and penultimate , Exchange the third and the penultimate …
You may ask :“ What if the number of characters in my string is odd ?”
The answer is , The character in the middle is exchanged with himself , It doesn't matter , He is still in his position .
void reverse_string(char* str)
{
int len = strlen(str);
char* left=str;
char* right = str + len - 1;
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[20] = {
0 };
printf(" Please enter the string you want in reverse order :");
scanf("%s", arr);
reverse_string(arr);
printf("%s", arr);
return 0;
}

recursive
The idea of recursion is very simple , It's going down layer by layer “ Deliver ”, Until the string becomes only one character , Then print that character
Then it goes up layer by layer “ return ”, Print the character locked by the pointer
void reverse_string(char* str)
{
if (*str != '\0')
{
reverse_string(str + 1);
}
printf("%c", *str);
}
int main()
{
char arr[20] = {
0 };
printf(" Please enter the string you want in reverse order :");
scanf("%s", arr);
reverse_string(arr);
return 0;
}

边栏推荐
- 数学规划分类 Math Programming Classfication
- 数据平台下的数据治理
- 3 词法分析
- 力矩电机控制基本原理
- If jimureport building block report is integrated according to the framework
- Some summary about function
- ArcGIS中的WKID
- Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display
- LabVIEW develops PCI-1680U dual port can card
- Scratch seamless butt bron filter
猜你喜欢

Data governance under data platform

Title: give a group of arrays, arranged from large to small and from small to large.

Win10 set up a flutter environment to step on the pit diary

MapGIS格式转ArcGIS方法

沃达德软件:智慧城市方案

LabVIEW 开发 PCI-1680U双端口CAN卡

C语言逆序打印字符串的两种方法

Visitor mode

【PMP学习笔记】第1章 PMP体系引论

LabVIEW develops PCI-1680U dual port can card
随机推荐
数学规划分类 Math Programming Classfication
Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR
What is the difference between character constants and string constants?
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
Xiaobai programmer's fourth day
Learning orientation today
3 词法分析
[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]
Internship: writing common tool classes
Smart S7-200 PLC channel free mapping function block (do_map)
Why is the integer type 128 to byte -128
Arcgis10.2 configuring postgresql9.2 standard tutorial
Data quality: the core of data governance
Math programming classification
【集训DAY13】Travel【暴力】【动态规划】
[PMP learning notes] Chapter 1 Introduction to PMP System
3dslicer import cone beam CT image
LabVIEW 开发 PCI-1680U双端口CAN卡
Formal parameters, arguments and return values in functions
win10搭建flutter环境踩坑日记