当前位置:网站首页>每日一题——倒置字符串
每日一题——倒置字符串
2022-07-02 15:22:00 【保护小周ღ】
题目来源于牛客网
描述
将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I
输入描述:
每个测试输入包含1个测试用例: I like beijing. 输入用例长度不超过100
输出描述:
依次输出倒置之后的字符串,以空格分割
示例1
输入:
I like beijing.
输出:
beijing. like I
解题思路:
先将字符串整体翻转,再对翻转后的字符串中的每一个单词部分进行二次翻转。
提示:字符串结束标识为 '\0'; 当我们遍历字符串遇到空格时,我们就可以解释为遇到了单词。
例如:I Like beijing.
第一步,字符串整体逆置,得到:.gnijieb ekil I
第二步,将字符串中的每一个单词逆置,得到:biejing. like I
博主为大家带来两种做法,一种是普通做法,适合编程初学者理解,一种是涉及到指针,通用函数用于翻转。
普通做法:
#include<stdio.h>
#include<string.h>
void FStr(char *Str, int n)
{
int i = n - 1;
//定义一个字符类型的数组a,用于存储翻转后的数据
char a[150] = { 0 };
while (*Str != '\0')
{
a[i]=*Str;
++Str;
--i;
}
//将整体翻转的字符串,拷贝至Str数组,改变原数据
strcpy(Str,a);
/*i = 0;
while (i<n)
{
Str[i] = a[i];
++i;
}*/
for (i=0;i<n-1;++i)
{
int j = i;
//遍历确定空格前的字符个数,可以理解为单词
while (a[j] != '\0' && a[j] != ' ')
{
++j;
}
int index = i;
//翻转空格前的字符,给到Str数组
while (j > index)
{
Str[i] = a[j-1];
--j;
++i;
}
}
//翻转完成打印Str
printf("%s\n",Str);
}
int main()
{
char Str[150]= {0};
//输入字符串
gets(Str);
//逆
FStr(Str,strlen(Str));
return 0;
}
进阶做法:
#include <stdio.h>
#include <string.h>
void FStr(char* begin, char* end)//翻转字符串
{
while(begin <=end)
{
char tmp =* begin;
* begin = *end;
*end= tmp;
++begin;
--end;
}
}
int main()
{
char str[100] ={0};
gets(str);
//记录字符串里字符的个数
int n = strlen(str);
//将字符串整体翻转
FStr(str, str+n-1);//str+n-1,相当于&str[n-1]
char* start = str;
//将字符串中的每个单词翻转
while(*start)
{
char* end = start;
//遍历确认空格前字符的个数
while(*end != ' ' && *end!='\0')//寻找空格或'\0'
{
end++;
}
//翻转空格、结束标志'\0'前的字符
FStr(start, end - 1);
if(*end == ' ')//跳过空格不参与翻转
{
start = end + 1;
}
else
{
start = end;
}
}
//打印
printf("%s",str);
return 0;
}
感兴趣的朋友可以点击链接尝试用博主的方法,或者是自己的方法做做。
题目来源于:牛客网
链接:倒置字符串__牛客网
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ
如有侵权请联系修改删除!
边栏推荐
- Microservice architecture practice: Construction of scalable distributed database cluster
- Map集合详细讲解
- Platform management background and merchant menu resource management: merchant role management design
- HBuilderX运行到手机或模拟器提示没有找到设备
- How openharmony starts fa (local and remote)
- 简单线性规划问题
- SSB threshold_ SSB modulation "suggestions collection"
- ROS知识点——消息过滤器 ( message_filters)
- Leetcode question brushing record | 933_ Recent requests
- A case study of college entrance examination prediction based on multivariate time series
猜你喜欢
easyswoole3.2重启不成功
智能垃圾桶(五)——点亮OLED
简单线性规划问题
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
深度之眼(三)——矩阵的行列式
每日一题——小乐乐改数字
HBuilderX运行到手机或模拟器提示没有找到设备
线性规划例题 投资的收益与风险
Sword finger offer 22 The penultimate node in the linked list
Blog theme "text" summer fresh Special Edition
随机推荐
13、Darknet YOLO3
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
Update iteration of cloud communication interface -- the official launch of subail API V4
Eth data set download and related problems
executescalar mysql_ExecuteScalar()
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
ETH数据集下载及相关问题
Microservice architecture practice: Construction of scalable distributed database cluster
从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)
CEPH principle
Briefly introduce the use of base64encoder
The difference between class and getClass ()
visibilitychange – 指定标签页可见时,刷新页面数据
Sword finger offer 24 Reverse linked list
Microservice architecture practice: using Jenkins to realize automatic construction
深度之眼(三)——矩阵的行列式
Leetcode question brushing record | 933_ Recent requests
[web technology] 1233 seconds understand web component
Idea2021.1 installation tutorial
简单线性规划问题