当前位置:网站首页>从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
边栏推荐
- [translation] the background project has joined the CNCF incubator
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
- Add MDF database file to SQL Server database, and the error is reported
- JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
- random shuffle注意
- Interview stereotyped version
- Gbase 8C trigger (I)
- 定了,就选它
- Compréhension simple de SVG
- [fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
猜你喜欢
Summary of interview project technology stack
Principle and application of database
基于can总线的A2L文件解析(2)
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Linear rectification function relu and its variants in deep learning activation function
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
sql server数据库添加 mdf数据库文件,遇到的报错
Kubernetes cluster log and efk architecture log scheme
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
随机推荐
Gbase 8C create user / role example 2
Gbase 8C system table PG_ conversion
GBase 8c系统表-pg_amproc
定了,就选它
Gbase 8C function / stored procedure parameters (I)
怎么将yolov5中的PANet层改为BiFPN
SqlServer行转列PIVOT
Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
Use cve-2021-43893 to delete files on the domain controller
搭建私有云盘 cloudreve
Process the dataset and use labelencoder to convert all IDs to start from 0
《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
Gbase 8C system table PG_ collation
Gbase 8C system table PG_ amop
HTB-Devel
Why choose a frame? What frame to choose
当lambda没有输入时,是何含义?
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文