当前位置:网站首页>[C language classic]: inverted string
[C language classic]: inverted string
2022-06-11 02:37:00 【Young master KC】
【C Language classics 】: Inverted string
List of articles
link : https://www.nowcoder.com/questionTerminal/b0ae3d72444a40e2bb14af7d4cb856ba
source : Cattle from
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
Thought analysis
We know by observation that , Reverse the whole string first , This problem can be solved by reversing each word in the string .
- Reverse the entire string
- Reverse the order of each word in the string
As shown in the figure

matters needing attention
Out of commission scanf, because scanf Abort as soon as a space is encountered , have access to gets(); perhaps gets_s();

gets_s() Use

First of all, the string is inversely independent into a function of inversely ordered characters
*str Is the first address of the string ,right Is the subscript of the last letter in the string ,
void reseve(char* str, int left, int right)
{
while (left < right)
{
char ch = str[left];
str[left] = str[right];// Exchange string
str[right] = ch;
left++;
right--;
}
}
Second, reverse the order of each word in the string
int temp = 0;// The subscript of the first element of the array
for (int i = 0; i < len + 1; i++).// Traversal string
{
if (arr[i] == ' ' || arr[i] == 0)// Encountered a space or \0 You need words in reverse order
{
reseve(arr, temp, i - 1);// Call the array in reverse order , i Is a space or \0 The subscript i-1 Subscript the last letter of a word
temp = i + 1;// The subscript of the first letter of the next word
}
}
All the code
#include<string.h> #include<stdio.h> void reseve(char* str, int left, int right) { while (left < right) { char ch = str[left]; str[left] = str[right]; str[right] = ch; left++; right--; } } int main() { char arr[100] = { 0 }; gets_s(arr,100); int len = strlen(arr); reseve(arr, 0, len - 1); int temp = 0; for (int i = 0; i < len + 1; i++) { if (arr[i] == ' ' || arr[i] == 0) { reseve(arr, temp, i - 1); temp = i + 1; } } printf("%s", arr); return 0; }
Conclusion
Upper couplet : The world is so big , I want to see it ; Lower bound : The wallet is so small , No one can leave ; Streamer : Go to work well .

边栏推荐
- ShaderGraphs
- Byte beating client R & D Intern Tiktok side
- Modify release opening animation
- Unity3d detects that the object is not within the range of the camera
- String operation methods: replace, delete and split strings
- Forest v1.5.22 发布!支持Kotlin
- Koa2 learning notes
- 【189. 轮转数组】
- P4338 [ZJOI2018]历史(树剖)(暴力)
- The most complete format description of clang format
猜你喜欢

Write my Ini configuration file error

Li Kou brushing questions - hash table

叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物

扁平数据转tree与tree数据扁平化

Flat data to tree and tree data flattening

When the interviewer opens his mouth, he comes to compose. Is this the case now?

CPT 102_LEC 17

Istio安装与使用

如何3步精读《PMBOK指南》(经验+资料分享)

SD3.0笔记
随机推荐
Unity determines whether the object is in the camera field of view
JS summary of math functions in math objects
Sd3.0 notes
Write my Ini configuration file error
[untitled]
String operation methods: replace, delete and split strings
SD3.0笔记
Project - redis message queue + worker thread fetches user operation logs and stores them (2)
企业展厅设计能为企业带来什么?
A collection of common ADB commands for app testing
【面试题 17.04. 消失的数字】
Project records
从绿联冲刺IPO,看手机配件市场沉浮录
Kotlin let方法
靠贴牌飞利浦冲击上市,德尔玛的自有品牌又该如何“起跳”?
动态给对象添加属性
SQL | return customer name, relevant order number and total price of each order
Forest v1.5.22 发布!支持Kotlin
APP测试_测试点总结
Istio installation and use