当前位置:网站首页>The last lesson of the first stage of C language: inverted string (for example: I like Beijing. Print as: Beijing. Like I)
The last lesson of the first stage of C language: inverted string (for example: I like Beijing. Print as: Beijing. Like I)
2022-04-22 21:12:00 【@You have to type the code every day】
Preface :Hello! Hello everyone , I am a @ Typing code every day ; Thank you for your support !
Until today , About C The content of the initial stage of language , We'll finish reviewing ; For the preliminary content, my focus is to let you know in the form of exercises . Next, let's review C The advanced part of language , Of course, I will continue to update some data structures . For advanced, most of them are Use of the pointer and Memory understanding , So we must learn the content of the pointer well , Or it'll be big in the back ! I will also talk about the advanced part in detail , Including some concepts and definitions 、 And the form of data stored in memory ! Today we will conclude with an example C Elementary language learning , I hope this example will make you gain something .

subject :

analysis :
This problem is obviously to use Flipping , How to flip ? How many times ? This may be difficult to grasp ; The method of this problem is similar to three-step translation and forwarding , We used to be in a rotating array ( Left and right ) Mentioned in , Interested friends can learn : Rotated array ( Left and right )
First step : First, we definitely need to get this string , use scanf obtain ? Of course not ,scanf Space encountered (" ") It will end , So here we need to use gets To receive the entire string , We directly gets( Array name ) perhaps gets_s( Array name , Array size ), Both are OK , The latter is relatively safer ! Pay attention to the header file <string.h>
The second step : We encapsulate a Rotation function reserve, Put the whole string in reverse order first ; This is very simple , We've met a lot of this before , I won't repeat .
The third step : We need to reverse the order of each word , How to control ? such as : Let's take the test cases given by the topic to analyze :I like beijing.
First of all, we can't move arr, need arr This first address controls printing !
So you need to define two pointers start and end,char* start = arr,char* end = start, We need to write two loops , A control start The pointer , A control end The pointer ;
as long as end The pointer did not encounter a space (" ") perhaps \0 let end++; When you jump out of the loop , Must be end Space encountered (" ") perhaps \0 了 , Let's ignore the others , First call the rotation function reserve, Rotate the previous word ; Then analyze , What kind of situation :
Case one : If you encounter spaces (" "), It means that the word is over , You need to skip this Space (" "), let start =end+1,start Just remember the starting position of the next word , Then continue to end++, Repeat this operation !
The second case : If you meet \0, Description to the end of the string , At this point, put end Assign a value to start,strat=end=\0, So you can exit the loop .
Step four : Let's just print it .
Code implementation :

To test :

summary :
This blog is a little less , I didn't think of it , But I think this question is quite classic , I hope you guys have something to gain , Thank you for your support ! Next time we will learn the advanced content . Learning together ! Progress together !

版权声明
本文为[@You have to type the code every day]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222109290858.html
边栏推荐
猜你喜欢
随机推荐
Chapter I Introduction to virtual reality technology
TC 结构管理器 - 打包 解包
JMeter complete set of data
(1) UART subsystem learning plan
QTP11.5/UFT含中文汉化包
新闻速递 I MobTech通过中国信通院“安全专项评测”
Study record of the 8th week of graduation project
HDLBits(十)学习笔记——有限状态机(FSM1 - Lemmings4)
2022起重机械指挥上岗证题库及在线模拟考试
LeetCode-238-除自身以外数组的乘积
Introduction and hardware implementation of sereds deserialization module
2022年R2移动式压力容器充装培训试题及在线模拟考试
Selenium web automated testing
Children's organization and ideological education in Central China Normal University
2022 electrician (elementary) examination question bank and online simulation examination
2022 R2 mobile pressure vessel filling training test questions and online simulation test
1、 Machine learning concepts
Summary of software testing knowledge points | JMeter implementation Interface Association
QT QString踩坑
Botu PLC user defined data type




![解数独[Pre-DS-hash结构 + component-DFS | Pre-hash-1-9特性--二进制状态压缩 + DFS]](/img/06/295b26f1389da3d90ad211dc447123.png)





