当前位置:网站首页>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软件安装详解
- 小满nestjs(第六章 nestjs cli 常用命令)
- Oracle 字段自增
- 【kali-密码攻击】(5.1.1)密码在线破解:Hydra(图形界面)
- How are data integration APIs key to enterprise digital transformation?
- 痛击面试官 CURD系统也能做出技术含量
- 智能家居设备安全分析技术综述
- 大健康产业商业供应链管理系统数字化提升产业链运作效率推动供应链标准化建设
- Cholesterol-PEG-Thiol,CLS-PEG-SH,胆固醇-聚乙二醇-巯基用于改善溶解度
- Toronto Research Chemicals加米霉素-d4说明书
猜你喜欢
明明加了唯一索引,为什么还是产生重复数据?
《评估、创建和使用知识图谱的限制》2022最新230页博士论文,根特大学
hdu 2094 产生冠军(STL map || 拓扑 || STL set)
小满nestjs(第四章 前置知识装饰器-实现一个GET请求)
Prometheus Operator 通过additional 添加target
华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款
使用Mock技术模拟数据
[] free column Android run Android, her - as command of safety
Win11搜索不到文件的解决方法
[Free column] APK dynamic reverse application of Android security [Three Smali injection methods]
随机推荐
[] free column Android run Android, her - as command of safety
为什么数字钱包需要引入小程序生态
How to deal with keys when Redis is large?
LeetCode每日一题(321. Create Maximum Number)
数学建模代码速成~赛前一个月~matlab~代码模板~吐血总结~三大模型代码(预测模型、优化模型、评价模型)
基于SSM实现手机销售商城系统
WPF中加载并使用图像资源
2.2 监督学习-1
继承关系下构造方法的访问特点
laravel之phpunit单元测试
【深度学习】pix2pix GAN理论及代码实现
poj 1182 食物链(带权并查集)
pytest框架之mark标记功能详细介绍
mysql duplicate data group multiple latest records
pat链表专题训练+搜索专题
看完这波 Android 面试题;助你斩获心中 offer
C语言之实现倒置字符串的两种方法
39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
Swift -- 数组高阶函数
Prometheus Operator 自定义监控添加redis explorer