当前位置:网站首页>从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
2022-07-03 02:44:00 【亖夕】
目录
题目:
输入一个字符串,判断该字符串是否为回文字符串,要求利用指针作为参数实现。
回文字符串是指:正向遍历和逆向遍历都相同得字符串。
示例 1:
输入:string = "ABBA"
输出:是回文字符串或True
示例 2:
输入:string = "雾锁山头山锁雾"
输出:是回文字符串或True
示例 3:
输入:string = “Hello word”
输出:不是回文数字或False
解释:正向遍历Hello word != drow olleH
题目分析:
回文字符串判断实际应用较少,作为练习题较为常见。
解题思路:
根据回文数的定义正向遍历和逆向遍历都相同得字符串。
那么我们可以使用指针S指向字符串的首个字符,我们将指针S称为首指针,再用指针E指向字符串的最后一个字符,指针E称为尾指针。
接着我们比较两指针指向的字符是否相同,不相同则直接输出该字符串不是回文字符串,相同则将首指针S后移一个位置使其指向下一个指针,重复上述步骤。
最后我们可以根据循环结束时指针的位置来判断该字符串是否为回文字符串,也可以根据循环结束阶段来判断该字符串是否为回文字符串。
代码实现
# include "stdio.h"
# include "string.h"
# define SIZE 100
int main()
{
void Justhw(char *p1, char *p2);
char string[SIZE], *pStart, *pEnd;
int n;
printf("enter string(len<=%d):", SIZE);
scanf("%s", string);
n = strlen(string);
pStart= &string[0];
pEnd = &string[n-1];
Justhw(pStart, pEnd);
return 0;
}
void Justhw(char *p1, char *p2)
{
while (p1 < p2) {
if (*p1 != *p2) {
printf("这不是回文字符串\n");// A
return;
}
p1++;
p2--;
}
// C
}代码注释
# include "stdio.h"
# include "string.h" // 使用strlen函数,导入其头文件
# define SIZE 100
int main()
{
void Justhw(char *p1, char *p2); // 初始化函数
char string[SIZE], *pStart, *pEnd; // 初始化指针
int n; // n表示字符串长度
printf("enter string(len<=%d):", SIZE);
scanf("%s", string); // 输入字符串
n = strlen(string); // strlen函数获取字符串的长度
// if ((n&1) == 1) {
// printf("这不是回文字符串\n");
// return 0;
// }
pStart= &string[0]; // 使首指针pStart指向字符串首元素
pEnd = &string[n-1]; // 使尾指针pEnd指向字符串尾元素
Justhw(pStart, pEnd); // 调用函数判断是否回文
return 0;
}
void Justhw(char *p1, char *p2)
{
while (p1 < p2) { // 循环停止条件为:尾指针 => 首指针,
// 因为当尾指针>首指针说明两指针已遍历完字符串且多移动了一步。
// 这里的等号主要是回文字符串为奇数个字符的时候,两指针相遇再最中间字符串
if (*p1 != *p2) {
printf("这不是回文字符串\n");// A
return; // 这里的return不返回任何值,我们使用return的目的是
// 为了当判断该字符串不是回文字符串时就停止程序,不执行下面的语句
}
p1++; // 首指针后移
p2--; // 尾指针前移
}
// C
printf("这是回文字符串\n"); // B
}如果是想根据循环结束时指针的位置判断是否回文则,将A、B位置的语句删除,在C处填入
if (p1-1 == p2 || p1 == p2) printf("这是回文字符串\n"); else printf("这不是回文字符串\n");若回文,则字符串长度奇偶情况一样
字符串长度为偶数时,循环结束后两指针移动相遇且多移动了一步,两指针还是相邻的,那么p1-1 = p2(等同于p1 = p2+1)
字符串长度为偶数时,两指针正好再最中间的字符相遇p1 = p2,循环结束后照样多移一步
今天就到这,下期见。
end
边栏推荐
- Gbase 8C trigger (II)
- Super easy to use logzero
- [translation] modern application load balancing with centralized control plane
- 【翻译】具有集中控制平面的现代应用负载平衡
- [fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
- 处理数据集,使用LabelEncoder将所有id转换为从0开始
- Serious security vulnerabilities reported by moxa mxview network management software
- Apple releases MacOS 11.6.4 update: mainly security fixes
- [C语言]给账号密码进行MD5加密
- Joking about Domain Driven Design (III) -- Dilemma
猜你喜欢

MATLAB小技巧(24)RBF,GRNN,PNN-神经网络

错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决

Random Shuffle attention
![[translation] the background project has joined the CNCF incubator](/img/0b/e3d2674b1a1cba3ea398cbcb1a018a.png)
[translation] the background project has joined the CNCF incubator

Kubernetes cluster log and efk architecture log scheme
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

HTB-Devel

Principle and application of database

Kubernetes cluster log and efk architecture log scheme
![[fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)](/img/6a/ae44ddb090ce6373f04a550a15f973.jpg)
[fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)
随机推荐
GBase 8c 创建用户/角色 示例二
Gbase 8C trigger (III)
怎么将yolov5中的PANet层改为BiFPN
Random shuffle note
SQL server queries the table structure of the specified table
处理数据集,使用LabelEncoder将所有id转换为从0开始
搭建私有云盘 cloudreve
GBase 8c 函数/存储过程定义
The left value and the right finger explain better
Process the dataset and use labelencoder to convert all IDs to start from 0
HW-初始准备
Pytest (6) -fixture (Firmware)
Gbase 8C system table PG_ constraint
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
【翻译】Flux安全。通过模糊处理获得更多信心
GBase 8c 函数/存储过程参数(一)
SQL statement
[translation] flux is safe. Gain more confidence through fuzzy processing
A2L file parsing based on CAN bus (2)
二维格式数组格式索引下标连续问题导致 返回json 格式问题


