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

边栏推荐
- ‘latin-1‘ codec can‘t encode characters in position 103-115: Body (‘一串中文‘) is not valid Latin-1
- Live broadcast review | how can the container transformation of traditional applications be fast and stable?
- 异构交易场景交互流程及一致性保证
- SSTable详解
- MySQL (VIII) - explain
- 301. delete invalid brackets
- Unet代码实现
- U-Net: Convolutional Networks for Biomedical Image Segmentation
- 295. median data flow
- Arthas-thread命令定位线程死锁
猜你喜欢

Learning and using quartz scheduling framework

基于51单片机的温度检测监测报警系统设计

RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check

对二进制的某一位操作

How flannel works

Deeplab V3 code structure diagram

NTU-RGBD数据集下载及数据格式解析

Nacos适配oracle11g-修改Nacos源码

在线JSON转CSharp(C#)Class工具

U-Net: Convolutional Networks for Biomedical Image Segmentation
随机推荐
How to quickly and gracefully download large files from Google cloud disk (II)
跳跃表原理
用户态和内核态
Spock约束-调用频率/目标/方法参数
Spock constraint - call frequency / target / method parameters
[AI practice] data normalization and standardization of machine learning data processing
启发式的搜索策略
Here comes the dry goods | PAAS collection to see first ~
Realization of rolling broadcast effect
Redis setting password
MySQL (11) - sorting out MySQL interview questions
作为思摩尔应对气候变化紧急事件的一项举措,FEELM加入碳披露项目
MySQL (VIII) - explain
313. super ugly number
链游飞船开发 农民世界链游开发 土地链游开发
数据库原理实验测试题,关于图书分类表
PSP code implementation
OSI分层模型对工作的具体帮助
Quartz调度框架的学习使用
【AI实战】机器学习数据处理之数据归一化、标准化