当前位置:网站首页>[C exercise] inverted string
[C exercise] inverted string
2022-07-18 01:41:00 【Domeecky】
example :
Input :I like beijing.
Output :beijing. like I
Method : We can reverse the whole string first , Then reverse each word inside .
#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse_arr(char * start ,char *end)
{
assert(start);
assert(end);
char tmp = *start;
*start = *end;
*end = '\0';
if (strlen(start) > 1)
{
reverse_arr(start + 1, end - 1);
};
*end = tmp;
}
void reverse_words(char* arr)
{
assert(arr);
char* start = arr;
char* end = arr;
while (*end != '\0')//end Point to '\0' when , Stop in reverse order
{
while (*end != ' ' && *end != '\0')// Find the space , And I promise not '\0'
{
end++;
}
// The reverse
reverse_arr(start, end - 1);//end Point to space , Spaces do not need to be in reverse order , therefore -1
if (*end != '\0')//end No '\0', be start Point to the new word after the space
start = ++end;
}
}
int main()
{
char arr[20] = "I like beijing.";
int end = strlen(arr);
// Reverse the entire string
reverse_arr(arr,arr+end-1);
// Single word in reverse order
reverse_words(arr);
printf("%s", arr);
return 0;
}边栏推荐
猜你喜欢

【C 练习】打印‘X’图形

C语言指针相关问题解析

数据传输:同构异IP数据源批量抽取实践

【C】 Address of array

Matlab bottom source code to realize image dynamic binarization

【C数据的存储】

【HCIA】OSI 模型

保持电气化时代的交通安全“零伤亡”,沃尔沃底气何来?
![[latex Chinese and English style & Bold] freely choose Chinese and English font styles, and set the bold and black degree of Chinese and English fonts](/img/36/db868e14b32b9f42c7c0dd9187d6a8.png)
[latex Chinese and English style & Bold] freely choose Chinese and English font styles, and set the bold and black degree of Chinese and English fonts

【C】 Creation and destruction of function stack frames
随机推荐
Détection des visages souriants basée sur l'apprentissage automatique
Common SQL statements, stored procedures and functions
今日睡眠质量记录80分
【C】 Creation and destruction of function stack frames
Network security (1)
To further improve the digital and intelligent level, Asia lion's first product is refreshed, with a price of 141500
C# 飞行棋小游戏
Calculation method engineering mathematics first lesson Doolittle
Node connects to the database for addition, deletion, modification and query
WinForm控件属性大全
Play with Nacos! Replace Eureka as configuration center and Registration Center
Matlab底层源代码实现图像腐蚀,膨胀操作(与Halcon效果一致)
AB test command
Realizing Halcon scale with MATLAB low-level source code_ image_ Max operator effect
pat刷题
Restore of data encryption returned by a website
redis事务和消息订阅发布
input仅允许输入英文、中文及其他各种符号文字
Cloud mall source code repair 30 sets of templates to support one click docking of major systems
【HCIA】OSI 模型