当前位置:网站首页>这道字符串反转的题目,你能想到更好的方法吗?
这道字符串反转的题目,你能想到更好的方法吗?
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;}

边栏推荐
- Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family
- ‘latin-1‘ codec can‘t encode characters in position 103-115: Body (‘一串中文‘) is not valid Latin-1
- 传智教育 | 多人协作开发出现代码冲突,如何合并代码?
- Heuristic search strategy
- Sstable details
- 897. incremental sequential search tree
- SSTable详解
- 在线JSON转CSharp(C#)Class工具
- In depth learning series 47:stylegan summary
- PSP code implementation
猜你喜欢
![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)
[* * * array * * *]

In depth learning series 47:stylegan summary

Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family

Both are hard disk partitions. What is the difference between C disk and D disk?

小爱音箱连接网络异常解决办法

【PyQt5系列】修改计数器实现控制

Flannel 工作原理
![[game theory] basic knowledge](/img/eb/08b1ce5106e574dc42be58f72fbab9.jpg)
[game theory] basic knowledge

TensorFlow中的数据类型

RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
随机推荐
NPM download error NPM err code error
About SQL: is there a way to fill in the null value in the field without adding fields on the basis of the original fields
MySQL summary
MySQL (IV) - MySQL storage engine
Akamai-1.75版本-_abck参数生成-js逆向分析
PSP代码实现
303. region and retrieval - array immutable
Paddle version problem
GloRe
898. subarray bitwise OR operation
[AI practice] xgbgressor model accelerates training and uses GPU to train xgbgressor in seconds
In depth learning series 46: face image super score gfp-gan
CIRIUM(睿思誉)逐渐成为航空公司二氧化碳排放报告的标准
MySQL (11) - sorting out MySQL interview questions
Flannel 工作原理
如何优雅的快速下载谷歌云盘的大文件 (二)
codeforce 158B Taxi
. H5 file forgets the database name and uses h5py to print
闫氏DP分析法
paddle版本问题