当前位置:网站首页>每日一题——倒置字符串
每日一题——倒置字符串
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;
}
感兴趣的朋友可以点击链接尝试用博主的方法,或者是自己的方法做做。
题目来源于:牛客网
链接:倒置字符串__牛客网
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ
如有侵权请联系修改删除!
边栏推荐
- 深度之眼(三)——矩阵的行列式
- Platform management background and merchant menu resource management: merchant role management design
- ssb门限_SSB调制「建议收藏」
- Chmod command principle and usage details [easy to understand]
- How openharmony starts fa (local and remote)
- How to create a new page for SAP Spartacus storefront
- Alibaba Tianchi SQL learning notes - Day3
- IPtables中SNAT、DNAT和MASQUERADE的含义
- From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
- traceroute命令讲解
猜你喜欢
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
Income and risk of linear programming example investment
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
TCP congestion control details | 2 background
Experience home office, feel the completion of the project | community essay solicitation
vector的底层模拟实现
阿里天池SQL学习笔记——DAY3
Eye of depth (III) -- determinant of matrix
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
ETH数据集下载及相关问题
随机推荐
线性规划例题 投资的收益与风险
智能垃圾桶(五)——点亮OLED
When the industrial Internet began to enter the deep-water area, it appeared more in the form of industry
JS20 数组扁平化
How openharmony starts fa (local and remote)
IPtables中SNAT、DNAT和MASQUERADE的含义
Microservice architecture practice: Construction of scalable distributed database cluster
AtCoder Beginner Contest 237 VP补题
Niuke JS2 file extension
ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
A case study of college entrance examination prediction based on multivariate time series
chmod命令原理及用法详解[通俗易懂]
Sword finger offer 25 Merge two sorted linked lists
Helm kubernetes package management tool
什么是软件开发中的 green field 和 brown field 模式 - 绿地开发和棕地开发
The difference of message mechanism between MFC and QT
Sword finger offer 24 Reverse linked list
Example nonlinear integer programming
[web technology] 1233 seconds understand web component
Eth data set download and related problems