当前位置:网站首页>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,有错误的地方欢迎指正.
边栏推荐
猜你喜欢

Database multi-table link query method

Regular Expressions for Shell Programming

经典面试题 之 TCP 三次握手/ 四次挥手

贝塞尔函数

启动报错:Caused by: org.apache.ibatis.binding.BindingException汇总解决

Suddenly want to analyze the mortgage interest rate and interest calculation

常见的四种电阻之间有什么不同?

Shell -- -- -- -- -- - common gadgets, sort and uniq, tr, the cut

What is the difference between the four common resistors?

编译器不同,模式不同,对结果的影响
随机推荐
二叉排序树的左旋与右旋
Analysis: Which method is used to build a stock quantitative trading database?
如何设计一个高并发系统?
量化投资者是如何获取实时行情数据的呢?
What drives the development of quantitative trading interfaces?
经典面试题 之 SQL优化
如何让你的量化交易系统具有概率优势,具有正向收益预期呢?
常见的数学物理方程
爱因斯坦的光子理论
相干光(光学)
Mysql两个引擎对比
SNR 信噪比
程序化交易规则对于整个交易系统有什么意义?
Servlet life cycle
OpenSSF的开源软件风险评估工具:Scorecards
How to List < Map> grouping numerical merge sort
大咖说·对话生态|当Confluent遇见云:实时流动的数据更有价值
如何灵活运用量化交易接口的优势取长补短?
单向链表几个比较重要的函数(包括插入、删除、反转等)
经典面试题 之 JVM调优