当前位置:网站首页>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;
}

边栏推荐
猜你喜欢

Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display

It's over. I went to work for three months and became bald

Usage of in in SQL DQL query

ThreadLocal summary (to be continued)

H5 lucky scratch lottery free official account + direct operation

Xiaobai programmer day 8

Data governance under data platform

Binder principle
![[training Day12] min ratio [DFS] [minimum spanning tree]](/img/f8/e37efd0d2aa0b3d79484b9ac42b7eb.png)
[training Day12] min ratio [DFS] [minimum spanning tree]

Xiaobai programmer's fourth day
随机推荐
Multi data source switching
If it is modified according to the name of the framework module
Method of converting MAPGIS format to ArcGIS
Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
【集训DAY12】Bee GO!【动态规划】【数学】
【数据库学习】Redis 解析器&&单线程&&模型
Internship: writing common tool classes
[training Day12] min ratio [DFS] [minimum spanning tree]
C语言逆序打印字符串的两种方法
QML module not found
(1) DDL, DML, DQL, DCL and common data types
Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin
Win10 set up a flutter environment to step on the pit diary
Synchronized and volatile
Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
【集训DAY11】Nescafe【贪心】
谷歌分析UA怎么转最新版GA4最方便
Xiaobai programmer's fourth day
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture