当前位置:网站首页>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 !
边栏推荐
- Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
- AtCoder Beginner Contest 237 VP补题
- 微信小程序 —— 上下浮动的箭头
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- 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
- The construction of scalable distributed database cluster and the partition design of oneproxy sub database
- 常用SQL语句(完整范例)
- Qstype implementation of self drawing interface project practice (II)
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
- Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
猜你喜欢

阿里天池SQL学习笔记——DAY3

Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?

Sword finger offer 22 The penultimate node in the linked list

From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)

The construction of scalable distributed database cluster and the partition design of oneproxy sub database

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

Si446 usage record (II): generate header files using wds3
![链表求和[dummy+尾插法+函数处理链表引用常见坑位]](/img/08/30e8ca2376104d648a82dca8a72c42.png)
链表求和[dummy+尾插法+函数处理链表引用常见坑位]

线性规划例题 投资的收益与风险

Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
随机推荐
ROS knowledge point - message_filters
云通信接口更新迭代——SUBMAIL API V4正式上线
Smart trash can (V) - light up OLED
Common SQL statements (complete example)
How to create a new page for SAP Spartacus storefront
[非线性控制理论]7_High gain and High Frequency
什么是敏捷开发流程
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
Eth data set download and related problems
TCP拥塞控制详解 | 2. 背景
OpenHarmony如何启动FA(本地和远程)
选择 SAP Spartacus 作为 SAP Commerce Cloud Storefront 实现框架的五个理由
[web technology] 1233 seconds understand web component
VirtualLab基础实验教程-7.偏振(2)
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
Si446 usage record (I): basic data acquisition
CEPH principle
ETH数据集下载及相关问题
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