当前位置:网站首页>Inverted order at the beginning of the C language 】 【 string (type I like Beijing. Output Beijing. Like I)
Inverted order at the beginning of the C language 】 【 string (type I like Beijing. Output Beijing. Like I)
2022-08-09 16:17:00 【Little busy week in _】
问题引入
将一句话的单词进行倒置,标点不倒置.比如 I like beijing. 经过函数后变为:beijing. like I
输入描述
:
每个测试输入包含1个测试用例: I like beijing. 输入用例长度不超过100输出描述
:
依次输出倒置之后的字符串,以空格分割: beijing. like I
一、思路
- The entire sentence can be reversed first,Reverse the order of each word;You can also reverse each word first,Reverse the entire sentence.
- Custom reverse order functionreverse(),无返回值;Its parameter list is put into the address of the beginning and end of the word or sentence that needs to be reversed(char* left, char* right);The function body uses a loop,An object that constantly exchanges head and tail addresses,每交换一次,The first address will be respectively+1,尾地址-1,Until the first address is greater than the last address, the exchange condition is not satisfied,交换结束.
- Find the address of the beginning and end of each word:First set the first and last addresses to the first address of the arrayarr,Next traverse the tail addressend,until the trailing address is a space,Indicates the end of the first word,The trailing address is the one before the space,即end-1;此时,The first address is updated to the one after a space,即end+1;以此循环,直到遍历到‘\0’(字符串结束标志)End the loop at the end of the sentence.按此方法,You can get the start and end addresses of each word.
- Find the address of the beginning and end of the entire sentence:The first address is the first address of the arrayarr,The tail address is the first address plus the length of the array minus one.
二、代码展示
代码如下:
#include <stdio.h>
#include<string.h>
#include<assert.h>
void reverse(char* left, char* right) {
assert(left);
assert(right);
while (left < right) {
char temp = *left;
*left = *right;
*right = temp;//Object to exchange the first address
left++;
right--;
}
}
int main() {
char arr[101] = {
0 };
gets(arr);
char* cur = arr;//定义指针变量cur存储arr首地址
while (*cur) {
//cur='\0'结束循环
char* start = cur;//定义start指针变量
char* end = cur;//定义end指针变量
while (*end != ' ' && *end != '\0') {
//endTraverse until a space or end of sentence stops
end++;
}
//对每个单词进行逆序
reverse(start, end - 1);
if (*end !='\0') {
cur = end + 1;//首地址cur更新
}
else {
cur = end;//cur='\0'
}
}
int len = strlen(arr);//计算数组长度
//Reverse the entire sentence
reverse(arr, arr + len - 1);
//输出
printf("%s\n", arr);
return 0;
}
speculative method
- The method uses recursion,Put it first each time a string is readarrdoes not print in,Then print backwards when the recursion goes back.
- 弊端:This method just reverses the output of the string by reading,Essentially the string is not stored for inversion.
代码如下:
#include<stdio.h>
void reverse() {
char *arr[101];
if (scanf("%s",arr)!= EOF) {
reverse();
printf("%s ", arr);
}
}
int main() {
reverse();
return 0;
}
提示:The first method is generally recommended,The second method only understands,Avoid errors in use in other situations!There are other ideas and methods can also be exchanged,有错误的地方欢迎指正.
边栏推荐
- 对于程序化交易,重在预测还是重在对策呢?
- 经典面试题 之 TCP 三次握手/ 四次挥手
- 6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
- 怎么才可以知道量化程序化交易是否有效?
- 数组学习笔记
- 【C语言初阶】求最小公倍数的三种方法
- What are the implications of programmatic trading rules for the entire trading system?
- Suddenly want to analyze the mortgage interest rate and interest calculation
- 回收站一直显示未清空的图标问题
- My MySQL database was attacked and deleted for ransom, forcing me to use all my might to recover data
猜你喜欢
随机推荐
百度开源e-chart初探
大咖说·对话生态|当Confluent遇见云:实时流动的数据更有价值
OpenSSF的开源软件风险评估工具:Scorecards
数据库多表链接查询的方式
Mongodb增加权限管理
How to achieve long-term benefits through the Tongdaxin quantitative trading interface?
In the process of quantitative trading, retail investors can do this
What is a template engine?What are the common template engines?Introduction to common commands of thymeleaf.
What are the misunderstandings about the programmatic trading system interface?
redis从入门到精通
CV复习:过拟合、欠拟合
[MySql]实现多表查询-一对一,一对多
[Basic version] Integer addition, subtraction, multiplication and division calculator
如何设计一个高并发系统?
英语议论文读写01 Business and Economics
生产者/消费者问题(线程信号)
突然想分析下房贷利率及利息计算
百度地图——鹰眼轨迹服务
VS2010:出现devenv.sln解决方案保存对话框
Two-dimensional array to realize the eight queens problem