当前位置:网站首页>这道字符串反转的题目,你能想到更好的方法吗?
这道字符串反转的题目,你能想到更好的方法吗?
2022-06-23 06:47:00 【嵌入式Linux,】
周末有一个朋友问了一个笔试题目,当时还直播写了答案,但是总觉得写得不够好,现在把题目放出来。大家看看有没有什么更好的解法
题目
有一个字符串,如下,要求对字符串做反转后输出
//input the sky is blue
//output blue is sky the
我的答案如下面的代码,我是直接定义成数组的来操作,如果定义成 char * str 的话,大家有没有什么思路呢?
#include "stdio.h"char input[] = {"the sky is blue"};//题目://input the sky is blue//output blue is sky thevoid reverseWords(char* s, int n) { char ch;
int i=0;
for (i=0; i<n/2; i++) {
ch = s[i];
s[i] = s[n-i-1];
s[n-i-1] = ch;
}}//eulb si yks ehtvoid reverseWords_by_space(char* s, int n) { int i = 0;
int len = 0;
for (i=0; i<n; i++) {
if (s[i] == ' ') {
reverseWords(s+i-len, len);
len = 0;
} else if (s[i] == '\0') {
reverseWords(s+i-len, len);
len = 0;
}
if (s[i] != ' ') {
++len;
}
}}int main(void) { printf("%s\n", input);
reverseWords(input,strlen(input));
reverseWords_by_space(input,sizeof(input));
printf("%s\n", input);
// 写完了,大家有不明白的评论下
return 0;}

边栏推荐
猜你喜欢

PSP代码实现

Simpledateformat thread safety issues

传智教育 | 多人协作开发出现代码冲突,如何合并代码?

Nacos适配oracle11g-修改Nacos源码

In depth learning series 47:stylegan summary

Sstable details

MYSQL牛客刷题

Both are hard disk partitions. What is the difference between C disk and D disk?
![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)
[* * * array * * *]

Yolov5 detecting small targets (with source code)
随机推荐
paddle版本问题
Principle of skip table
Cirium has gradually become the standard for airlines' carbon dioxide emission reporting
npm下载报错npm ERR code ERESOLVE
csrf攻击在laravel中如何解决
Tp6+redis+think-queue+supervisor implements the process resident message queue /job task
Pspnet complete code implementation
100 GIS practical application cases (79) - key points of making multi plan integrated base map
char和varchar区别
Nacos适配oracle11g-建表ddl语句
deeplab v3 代码结构图
SimpleDateFormat 线程安全问题
[AI practice] xgb Xgbregression multioutputregressor parameter 1
MySQL (V) - locks and transactions
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
Spock sub piling
897. incremental sequential search tree
898. subarray bitwise OR operation
NPM download error NPM err code error
Akamai-1.75版本-_abck参数生成-js逆向分析