当前位置:网站首页>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 !
边栏推荐
- visibilitychange – 指定标签页可见时,刷新页面数据
- Sword finger offer 22 The penultimate node in the linked list
- 阿里天池SQL学习笔记——DAY3
- Alibaba Tianchi SQL learning notes - Day3
- Meanings of SNAT, DNAT and masquerade in iptables
- 【网络是怎样连接的】第五章 探索服务器
- Navigateur Chrome pour un accès rapide au stackoverflow
- 维护万星开源向量数据库是什么体验
- [shutter] dart data type (dynamic data type)
- Update iteration of cloud communication interface -- the official launch of subail API V4
猜你喜欢
随机推荐
Si446 usage record (I): basic data acquisition
Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
Dstat use [easy to understand]
Solving simple differential equations
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
easyswoole3.2重启不成功
牛客 JS3 分隔符
牛客 JS3 分隔符
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
RK1126平台项目总结
[非线性控制理论]7_High gain and High Frequency
如何给 SAP Spartacus Storefront 创建新的页面
chrome瀏覽器快速訪問stackoverflow
What is agile development process
Eth data set download and related problems
海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
Platform management background and business menu resource management: business permissions and menu resource management design
Migrate your accelerator based SAP commerce cloud storefront to Spartacus
What are the green field and brown field models in software development - green field development and brown field development
Vscode knowledge points - Common Errors