当前位置:网站首页>从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
边栏推荐
- Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
- sql server 查詢指定錶的錶結構
- javeScript 0.1 + 0.2 == 0.3的问题
- Packing and unpacking of JS
- [hcia]no.15 communication between VLANs
- GBase 8c系统表-pg_amop
- Concrete CMS vulnerability
- HTB-Devel
- GBase 8c 函数/存储过程定义
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
猜你喜欢

HTB-Devel

怎么将yolov5中的PANet层改为BiFPN

Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???

基于can总线的A2L文件解析(2)

Practice of traffic recording and playback in vivo

The Linux server needs to install the agent software EPS (agent) database

【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)

where 1=1 是什么意思

"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b

How to change the panet layer in yolov5 to bifpn
随机推荐
面试八股文整理版
Use optimization | points that can be optimized in recyclerview
GBase 8c 函数/存储过程参数(一)
Your family must be very poor if you fight like this!
Gbase 8C system table PG_ auth_ members
Gbase 8C system table PG_ authid
GBase 8c 函数/存储过程参数(二)
Pytest (6) -fixture (Firmware)
Gbase 8C system table PG_ conversion
sql server 查詢指定錶的錶結構
Gbase 8C system table PG_ collation
Gbase 8C system table PG_ cast
二维格式数组格式索引下标连续问题导致 返回json 格式问题
Source code analysis | layout file loading process
GBase 8c系统表-pg_amop
《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
How to change the panet layer in yolov5 to bifpn
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
[translation] modern application load balancing with centralized control plane
sql server 查询指定表的表结构


