当前位置:网站首页>Can you think of a better way to solve the problem of string inversion?
Can you think of a better way to solve the problem of string inversion?
2022-06-23 07:35:00 【Embedded Linux,】
At the weekend, a friend asked a written test question , At that time, I wrote the answer live , But I always feel that my writing is not good enough , Now let's put the topic out . Let's see if there is a better solution
subject
There's a string , as follows , Require the string to be inverted and output
//input the sky is blue
//output blue is sky the
My answer is like the following code , I directly define it as an array to operate , If it's defined as char * str Words , Do you have any ideas ?
#include "stdio.h"char input[] = {"the sky is blue"};// subject ://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);
// Finished writing , If you have any comments you don't understand
return 0;}

边栏推荐
- Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
- NPM download error NPM err code error
- MySQL summary
- PSP code implementation
- Database principle experiment test questions, about book classification table
- [AI practice] xgb Xgbregression multioutputregressor parameter 2 (GPU training model)
- Spock constraint - call frequency / target / method parameters
- Arthas thread command locates thread deadlock
- 【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
- Several characteristics of MySQL database
猜你喜欢

Solutions to abnormal network connection of Xiaoai speakers

20bn Jester complete dataset Download

如何优雅的快速下载谷歌云盘的大文件 (二)

Pagoda forgot password

What is customer experience automation?

跳跃表原理

How to quickly and gracefully download large files from Google cloud disk (II)

MYSQL牛客刷题
![[interface automation] software testing the core skills of salary increase to increase salary by 200%](/img/22/be8c5c922307225c34f6205f189c33.png)
[interface automation] software testing the core skills of salary increase to increase salary by 200%

In depth learning series 46: face image super score gfp-gan
随机推荐
Realization of rolling broadcast effect
RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
Solutions to abnormal network connection of Xiaoai speakers
Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
SSM integration
论文写作之WPS安装Mathtype插件编写数学公式
csrf攻击在laravel中如何解决
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
Live broadcast review | how can the container transformation of traditional applications be fast and stable?
Arthas-thread命令定位线程死锁
这道字符串反转的题目,你能想到更好的方法吗?
Mysql数据库的几个特点
Akamai-1.75 version-_ Abck parameter generation JS reverse analysis
PSP code implementation
[game theory] basic knowledge
Pagoda forgot password
Redis setting password
Nacos adapts to oracle11g- modify the source code of Nacos
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1