当前位置:网站首页>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 !
边栏推荐
- Blog theme "text" summer fresh Special Edition
- ceph 原理
- TCP拥塞控制详解 | 2. 背景
- [shutter] dart data type (dynamic data type)
- RK1126平台项目总结
- si446使用记录(二):使用WDS3生成头文件
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
- Chrome browser quick access stackoverflow
- 云通信接口更新迭代——SUBMAIL API V4正式上线
- SAP commerce cloud storefront framework selection: accelerator or Spartacus?
猜你喜欢
[非线性控制理论]8_三种鲁棒控制器的比较
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
13、Darknet YOLO3
About me
si446使用记录(一):基本资料获取
关于我
Si446 usage record (I): basic data acquisition
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
阿里天池SQL学习笔记——DAY3
Sword finger offer 26 Substructure of tree
随机推荐
About me
[web technology] 1233 seconds understand web component
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
What is agile development process
13、Darknet YOLO3
RK1126平台项目总结
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
简单线性规划问题
ETH数据集下载及相关问题
Navigateur Chrome pour un accès rapide au stackoverflow
Helm kubernetes package management tool
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
The difference of message mechanism between MFC and QT
详细介绍scrollIntoView()方法属性
si446使用记录(一):基本资料获取
第十五章 字符串本地化和消息字典(一)
SSB threshold_ SSB modulation "suggestions collection"
871. Minimum refueling times
Vscode + eslint configuration