当前位置:网站首页>Daily question - inverted string
Daily question - inverted string
2022-07-02 17:39:00 【Protect Xiaozhou】
The topic comes from niuke.com
describe
Invert the words of a sentence , Punctuation is not inverted . such as I like beijing. After passing through the function, it becomes :beijing. like I
Input description :
Each test input contains 1 Test cases : I like beijing. The length of the input case shall not exceed 100
Output description :
Output the inverted string in turn , Split by space
Example 1
Input :
I like beijing.
Output :
beijing. like I
Their thinking :
First flip the whole string , Then flip each word part of the flipped string twice .
Tips : The end of the string is marked '\0'; When we traverse the string and encounter spaces , We can interpret it as encountering words .
for example :I Like beijing.
First step , The whole string is inverted , obtain :.gnijieb ekil I
The second step , Invert every word in the string , obtain :biejing. like I
Bloggers bring you two ways , One is common practice , Suitable for beginners to understand programming , One involves pointers , General functions are used to flip .
Common practice :
#include<stdio.h>
#include<string.h>
void FStr(char *Str, int n)
{
int i = n - 1;
// Defines an array of character types a, Used to store flipped data
char a[150] = { 0 };
while (*Str != '\0')
{
a[i]=*Str;
++Str;
--i;
}
// The string that flips the whole , Copy to Str Array , Change the original data
strcpy(Str,a);
/*i = 0;
while (i<n)
{
Str[i] = a[i];
++i;
}*/
for (i=0;i<n-1;++i)
{
int j = i;
// Traverse to determine the number of characters before the space , It can be understood as the word
while (a[j] != '\0' && a[j] != ' ')
{
++j;
}
int index = i;
// Flip the character before the space , Give to the Str Array
while (j > index)
{
Str[i] = a[j-1];
--j;
++i;
}
}
// Flip to finish printing Str
printf("%s\n",Str);
}
int main()
{
char Str[150]= {0};
// Input string
gets(Str);
// The inverse
FStr(Str,strlen(Str));
return 0;
}
Advanced approach :
#include <stdio.h>
#include <string.h>
void FStr(char* begin, char* end)// Flip strings
{
while(begin <=end)
{
char tmp =* begin;
* begin = *end;
*end= tmp;
++begin;
--end;
}
}
int main()
{
char str[100] ={0};
gets(str);
// Record the number of characters in the string
int n = strlen(str);
// Flip the string as a whole
FStr(str, str+n-1);//str+n-1, amount to &str[n-1]
char* start = str;
// Flip each word in the string
while(*start)
{
char* end = start;
// Traverse to confirm the number of characters before the space
while(*end != ' ' && *end!='\0')// Look for spaces or '\0'
{
end++;
}
// Flip space 、 End mark '\0' The first character
FStr(start, end - 1);
if(*end == ' ')// Skipping spaces does not participate in flipping
{
start = end + 1;
}
else
{
start = end;
}
}
// Print
printf("%s",str);
return 0;
}
Interested friends can click on the link to try to use the blogger's method , Or do it in your own way .
The title comes from : Cattle from
link : Inverted string __ Cattle from
Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ
If there is infringement, please contact to modify and delete !
边栏推荐
- LeetCode:1380. Lucky number in matrix -- simple
- Use of nexttile function in MATLAB
- SSB threshold_ SSB modulation "suggestions collection"
- Uniapp H5 page calls wechat payment
- Platform management background and merchant menu resource management: merchant role management design
- JDBC
- visibilitychange – 指定标签页可见时,刷新页面数据
- 13、Darknet YOLO3
- Map集合详细讲解
- 关于我
猜你喜欢
每日一题——“水仙花数”
Use of nexttile function in MATLAB
Platform management background and merchant menu resource management: merchant role management design
Sword finger offer 27 Image of binary tree
线性规划例题 投资的收益与风险
easyswoole3.2重启不成功
Sword finger offer 26 Substructure of tree
【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
随机推荐
牛客 JS3 分隔符
freemarker+poi实现动态生成excel文件及解析excel文件
Flutter: 动作反馈
Briefly introduce the use of base64encoder
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
【目标跟踪】|SiamFC
Helm kubernetes package management tool
Map集合详细讲解
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
easyswoole3.2重启不成功
executescalar mysql_ExecuteScalar()
Schoolbag novel multithreaded crawler [easy to understand]
SSB threshold_ SSB modulation "suggestions collection"
What is agile development process
牛客JS2 文件扩展名
[web technology] 1233 seconds understand web component
Income and risk of linear programming example investment
traceroute命令讲解
JS20 array flattening
Timing / counter of 32 and 51 single chip microcomputer