当前位置:网站首页>From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
2022-07-03 02:46:00 【In the evening】
Catalog
subject :
Enter a string , Judge whether the string is palindrome string , It is required to use pointers as parameters .
Palindrome string refers to : positive Ergodic and reverse Traverse the same string .
Example 1:
Input :string = "ABBA"
Output : Is a palindrome string or True
Example 2:
Input :string = " Fog locks the mountain "
Output : Is a palindrome string or True
Example 3:
Input :string = “Hello word”
Output : Not palindrome numbers or False
explain : Positive traversal Hello word != drow olleH
Topic analysis :
Palindrome string judgment is rarely used in practice , As an exercise, it is common .
Their thinking :
According to the definition of palindrome number positive Ergodic and reverse Traverse the same string .
Then we can use pointers S Points to the first character of the string , We will point S Call it the leading pointer , Use the pointer E Points to the last character of the string , The pointer E Called tail pointer .
Then we compare whether the characters pointed to by the two pointers are the same , If not, the string is directly output instead of palindrome string , If the same, the first pointer S Move back one position so that it points to the next pointer , Repeat the above steps .
Finally, we can judge whether the string is a palindrome string according to the position of the pointer at the end of the loop , You can also judge whether the string is a palindrome string according to the end stage of the cycle .
Code implementation
# 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(" This is not a palindrome string \n");// A
return;
}
p1++;
p2--;
}
// C
}code annotation
# include "stdio.h"
# include "string.h" // Use strlen function , Import its header file
# define SIZE 100
int main()
{
void Justhw(char *p1, char *p2); // Initialization function
char string[SIZE], *pStart, *pEnd; // Initialization pointer
int n; // n Represents string length
printf("enter string(len<=%d):", SIZE);
scanf("%s", string); // Input string
n = strlen(string); // strlen Function to get the length of the string
// if ((n&1) == 1) {
// printf(" This is not a palindrome string \n");
// return 0;
// }
pStart= &string[0]; // Make the first pointer pStart Point to the first element of the string
pEnd = &string[n-1]; // Make the tail pointer pEnd Point to the element at the end of the string
Justhw(pStart, pEnd); // Call the function to determine whether the palindrome
return 0;
}
void Justhw(char *p1, char *p2)
{
while (p1 < p2) { // The cycle stop condition is : Tail pointer => Head pointer ,
// Because when the tail pointer > The first pointer indicates that the two pointers have traversed the string and moved one more step .
// The equal sign here is mainly when the palindrome string is an odd number of characters , When two pointers meet, the middle string
if (*p1 != *p2) {
printf(" This is not a palindrome string \n");// A
return; // there return No value returned , We use return Is the purpose of
// In order to stop the program when it is judged that the string is not a palindrome string , Do not execute the following statement
}
p1++; // The first pointer moves back
p2--; // The tail pointer moves forward
}
// C
printf(" This is the palindrome string \n"); // B
}If you want to judge whether to palindrome according to the position of the pointer at the end of the loop , take A、B Position statement deletion , stay C Fill in
if (p1-1 == p2 || p1 == p2) printf(" This is the palindrome string \n"); else printf(" This is not a palindrome string \n");If palindrome , Then the string length parity is the same
When the string length is even , After the cycle ends, the two pointers move to meet and move one more step , The two pointers are still adjacent , that p1-1 = p2( Equate to p1 = p2+1)
When the string length is even , The two pointers just meet the middle character p1 = p2, After the cycle ends, move one more step
That's it today , See you next time .
end
边栏推荐
- Today, it's time to copy the bottom!
- What does "where 1=1" mean
- MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
- Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
- Gbase 8C system table PG_ cast
- Use optimization | points that can be optimized in recyclerview
- sql server数据库添加 mdf数据库文件,遇到的报错
- Gbase 8C system table PG_ authid
- Joking about Domain Driven Design (III) -- Dilemma
- xiaodi-笔记
猜你喜欢

Mathematical statistics -- Sampling and sampling distribution
![[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)](/img/e5/c01f760b07b495f5b048ea367e0c21.gif)
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)

搭建私有云盘 cloudreve

Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution

A2L file parsing based on CAN bus (2)

Practice of traffic recording and playback in vivo

【翻译】具有集中控制平面的现代应用负载平衡
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)

Pytest (6) -fixture (Firmware)

面试项目技术栈总结
随机推荐
Super easy to use logzero
Add automatic model generation function to hade
MUX VLAN Foundation
SQL server queries the table structure of the specified table
The Linux server needs to install the agent software EPS (agent) database
Kubernetes family container housekeeper pod online Q & A?
yii2 中andWhere多个or查询 orm条件
Javescript 0.1 + 0.2 = = 0.3 problem
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
Gbase 8C system table PG_ class
定了,就选它
Use optimization | points that can be optimized in recyclerview
GBase 8c 函数/存储过程定义
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
tensor中的append应该如何实现
Choose it when you decide
[C语言]给账号密码进行MD5加密
Strategy application of Dameng database
Gbase 8C system table PG_ auth_ members
Use cve-2021-43893 to delete files on the domain controller


