当前位置:网站首页>Two methods of implementing inverted strings in C language
Two methods of implementing inverted strings in C language
2022-08-09 22:07:00 【Code Chen Shuai】
目录
1.Create an array to store the input string
How the statement ends the loop:
The start and end positions of each word :
How to judge the end of the sentence,To stop the reverse order
五、The second method inverts the string
前言:
Today I brushed a question on Niuke.com,I feel quite meaningful,Now I will share it with you,And tell everyone how to solve the problem.
一、题目

事例:
输入下列字符:
I like beijing.
输出的字符:
beijing. like I
二、思路讲解
1、Create an array to store the input string
2、Reverse each of these words 得到 I ekil .gnjieb
3、Reverse the string again 得到beijing.like I
三、代码实现
1.Create an array to store the input string
#include <stdio.h>
int main()
{
char arr[100] = { 0 };
gets(arr);//注意没有使用scanf,因为scanfWhen a space is encountered, it stops accepting input
printf("%s\n",arr);
return 0;
}2、Invert each word
这其中需要注意几点:
1.How the whole statement ends the loop
2.The start and end positions of each word
3.内部如何实现
4. 判断语句结束,Thereby stopping the reverse order
How the statement ends the loop:
char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
while (*t!='\0') {
//判断当*t指向'\0’时循环结束.
}The start and end positions of each word :
char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
while (*t!='\0') { //判断当*t指向'\0’时循环结束.
char* start = t;
char* end = t;
//When the end pointer points to a space,并且指向‘\0’循环结束.
while (*end != ' '&&*end != '\0') {
end++;
}内部怎么实现:
We need to define another function ourselvesreverse,Used to implement the exchange of strings
void reverse(char* left, char* right) {
while (left < right) {//Only if the address on the left is less than the address on the right,才交换,The middle ones do not need to be exchanged
char tmp = *right;
*right = *left;
*left = tmp;
left++;
right--;
}
}再调用这个函数:
int main() {
char arr[100] = { 0 };
gets(arr); //输入字符数组
char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
while (*t!='\0') { //判断当*t指向'\0’时循环结束.
char* start = t;
char* end = t;
//When the end pointer points to a space,并且指向‘\0’循环结束.
while (*end != ' '&&*end != '\0') {
end++;
}
reverse(start, end - 1);
}How to judge the end of the sentence,To stop the reverse order
if (*end != '\0')//Check if the end of the string is reached
t = end + 1;//让指针p指向下一个单词,Let the loop go to the next word
else
t = end;
3、将字符串逆置
This is used to calculate the length of the stringstrlen函数,
int len = strlen(arr);再调用reverse函数:
reverse(arr, arr + len - 1);四、代码总结
void reverse(char* left, char* right) {
while (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
left++;
right--;
}
}
int main() {
char arr[100] = { 0 };
gets(arr); //输入字符数组
char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
while (*t!='\0') { //判断当*t指向'\0’时循环结束.
char* start = t;
char* end = t;
//When the end pointer points to a space,并且指向‘\0’循环结束.
while (*end != ' '&&*end != '\0') {
end++;
}
reverse(start, end - 1);
if (*end != '\0')//Check if the end of the string is reached
t = end + 1;//让指针p指向下一个单词,Let the loop go to the next word
else
t = end;
}
int len = strlen(arr);
reverse(arr, arr + len - 1);
printf("%s\n", arr);
return 0;
}五、The second method inverts the string
The idea of method 2 is to use it firstt指向字符串的末尾,然后往前走,找到空格时,printf一下,以%s格式,这样打印只需要给字符串的首地址,它打印到\0停下.我们打印完一个单词后,把t的位置变成\0,然后继续往前走,直到数组开始位置.
t指针开始指向字符串最后一个字符,然后往前遍历,直到空格或者t到了最开始的字符位置,这里分两种情况,到空格位置,要Change the space position to \0,打印cur+1位置,到首字符,打印cur位置.
其中需要注意一点,我们Change the space position to 了\0,所以在第一种情况需要打印的时候在加个空格
代码如下:
int main()
{
char arr[100] = { 0 };
gets(arr);
char* t = arr + strlen(arr) - 1; //将tPointer to the last character of the string
while (t > arr) //当tThe address of the pointer is greater thanarrThe first element address starts the loop
{
while (*t != ' ' && t > arr) //当t不为空格时,tThe pointer loops forward
{
t--;
}
if (t == arr)
{
printf("%s", t);
}
else //到空格,打印t+1位置
{
printf("%s ", t + 1);
*t = '\0'; //Change the space position to ‘\0’
}
}
return 0;
}六、总结
今天的分享就到这了,The first method uses arrays,字符串逆置,容易想到,第二种方法,Difficulty thinking,代码简单,两种方法都可以,今天的分享就到这了,谢谢大家的支持.
边栏推荐
- win10配置CenterNet环境
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- Can I make a TCP connection without accept?
- Openharmony Lightweight System Experiment--GPIO Lighting
- DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
- hdu 2094 产生冠军(STL map || 拓扑 || STL set)
- ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
- SqlServer 2016 备份和还原
- 力扣383-赎金信——哈希映射数组法
- 如何在WPF中设置Grid ColumnDefinitions的样式
猜你喜欢
随机推荐
[] free column Android run Android, her - as command of safety
buuctf(探险2)
一种基于连接和安全熵的网络空间整体安全认识和方法
shell之变量详解,让你秒懂!
visual studio 2022调试技巧介绍
启动 CM agent 报错——ImportError: libssl.so.10: cannot open shared object file: No such file or directory
Queue topic: Implementing stacks with queues
[Free column] APK dynamic reverse application of Android security [Three Smali injection methods]
Openharmony Lightweight System Experiment--GPIO Lighting
小满nestjs(第六章 nestjs cli 常用命令)
【二叉树】树的子结构
日期及时间处理包 Carbon 在 Laravel 中的简单使用[通俗易懂]
AttributeError: module 'click' has no attribute 'get_os_args'
源码编译安装与yum和rpm软件安装详解
【图文并茂】如何进行Win7系统的重装
典型的数据仓库模型实施过程详解
Cholesterol-PEG-Thiol,CLS-PEG-SH,胆固醇-聚乙二醇-巯基用于改善溶解度
智能家居设备安全分析技术综述
请问一下flink cdc mysql source 报这种错怎么处理呢?我都设置了useSSL=f
What are the benefits of enterprise data integration?How do different industries solve the problem of data access?








