当前位置:网站首页>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,代码简单,两种方法都可以,今天的分享就到这了,谢谢大家的支持.
边栏推荐
- 源码编译安装与yum和rpm软件安装详解
- 基于SSM实现手机销售商城系统
- CMake installation upgrade higher version
- 2.3 监督学习-2
- 纸业供应链协同管理系统:重构纸业智慧供应网络,支撑企业数字化转型升级
- Prometheus Operator 通过additional 添加target
- [Free column] Xposed plug-in development for Android security [from scratch] tutorial
- PyTorch框架的 torch.cat()函数
- 毕昇编译器优化:Lazy Code Motion
- 为什么数字钱包需要引入小程序生态
猜你喜欢

mysql duplicate data group multiple latest records

leetcode 二叉树的分层遍历1

DP-Differential Privacy概念介绍

小满nestjs(第六章 nestjs cli 常用命令)

Toronto Research Chemicals加米霉素-d4说明书

Toronto Research Chemicals单羟基舒更葡糖钠说明书

企业数据打通有什么好处?不同行业怎么解决数据打通难题?

钢材行业供应链协同管理系统提升企业上下游密切度,精细化企业内部管理

source install/setup.bash时出现错误

【IoT毕设】STM32与机智云自助开发平台的宠物智能喂养系统
随机推荐
这年头还不来尝试线稿图视频??
明明加了唯一索引,为什么还是产生重复数据?
新出现的去中心化科学能够为科学领域带来什么?
[] free column Android dynamic debugging GDB APP of safety
安装多版本php(php5.6,php7.2)
Swift -- 数组高阶函数
IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN
痛击面试官 CURD系统也能做出技术含量
DSPE-PEG-Azide,DSPE-PEG-N3,磷脂-聚乙二醇-叠氮可和DBCO直接反应
访问控制知识
Toronto Research Chemicals加米霉素-d4说明书
PyTorch框架的 torch.cat()函数
面试官:Redis 大 key 要如何处理?
39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
MySQL, which is asked on both sides of the byte, almost didn't answer well
hdu 2647 Reward(拓扑排序)
Win11搜索不到文件的解决方法
Js查找字符串中出现最多次数的字母和单词
hdu 1285 确定比赛名次(拓扑排序)
php安装make出现“collect2:error:ldreturned1exitstatus