当前位置:网站首页>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,代码简单,两种方法都可以,今天的分享就到这了,谢谢大家的支持.
边栏推荐
- OpenSSL SSL_read: Connection was reset, errno 10054
- 解决执行Command报错executable file not found in $PATH
- 使用Mock技术模拟数据
- MySQL笔记-06 基础SQL操作
- 智能家居设备安全分析技术综述
- CMake installation upgrade higher version
- [Free column] Xposed plug-in development for Android security [from scratch] tutorial
- WPF中加载并使用图像资源
- 以技术创新加速国家“碳中和”建设进程,华为云创新中心助力欣冠精密实现云智控“气”
- IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN
猜你喜欢
随机推荐
AttributeError: module 'click' has no attribute 'get_os_args'
面试官:Redis 大 key 要如何处理?
如何从800万数据中快速捞出自己想要的数据?
中英文说明书丨Abbkine细胞迁移分析试剂盒
EsgynDB Troubleshooting - ERROR[2012] Server process tdm_arkesp could not becreated
leetcode 二叉树的公共近祖先
获取数组最后一项别再用array.length-1了
力扣 899. 有序队列
PCL学习之滤波Filtering
Haven't tried line art videos this year??
pat链表专题训练+搜索专题
顺序表的定义和基本操作
MySQL Notes-06 Basic SQL Operations
Oracle 字段自增
DP-Differential Privacy概念介绍
OpenSSL SSL_read: Connection was reset, errno 10054
渗透测试-对新型内存马webshell的研究
典型的数据仓库模型实施过程详解
奥特曼卡牌隐藏的百亿市场
【二叉树】树的子结构









