当前位置:网站首页>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,有错误的地方欢迎指正.
边栏推荐
猜你喜欢
随机推荐
常见的数学物理方程
怎么才可以知道量化程序化交易是否有效?
流程控制学习
CV复习:过拟合、欠拟合
Shell programming loop statement
如何灵活运用量化交易接口的优势取长补短?
复数与复数域
What is the difference between the four common resistors?
运算符学习
对于程序化交易,重在预测还是重在对策呢?
在量化交易过程中,散户可以这样做
启动报错:Caused by: org.apache.ibatis.binding.BindingException汇总解决
经典面试题 之 SQL优化
方法学习笔记
用户如何正确去认识程序化交易?
Database multi-table link query method
Servlet的生命周期
排序方法(希尔、快速、堆)
ImageWatch无法显示图像
深刻地认识到,编译器会导致编译结果的不同






![[MySql] implement multi-table query - one-to-one, one-to-many](/img/7e/8f1af4422a394969b28a553ead2c42.png)

