当前位置:网站首页>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
边栏推荐
- GBase 8c系统表-pg_class
- 疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
- Gbase 8C system table PG_ cast
- Random shuffle note
- 搭建私有云盘 cloudreve
- Serious security vulnerabilities reported by moxa mxview network management software
- 定了,就选它
- Monitoring and management of JVM
- Informatics Olympiad one general question bank 1006 a+b questions
- [translation] flux is safe. Gain more confidence through fuzzy processing
猜你喜欢

【翻译】后台项目加入了CNCF孵化器

Linear rectification function relu and its variants in deep learning activation function

What is the way out for children from poor families?

Super easy to use logzero
![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

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

C语言初阶-指针详解-庖丁解牛篇

Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes

random shuffle注意

Random Shuffle attention
随机推荐
Practice of traffic recording and playback in vivo
【翻译】具有集中控制平面的现代应用负载平衡
Kubernetes family container housekeeper pod online Q & A?
基于can总线的A2L文件解析(2)
tensor中的append应该如何实现
Joking about Domain Driven Design (III) -- Dilemma
Can netstat still play like this?
Wechat - developed by wechat official account Net core access
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
Cvpr2022 remove rain and fog
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
Cancer biopsy instruments and kits - market status and future development trends
处理数据集,使用LabelEncoder将所有id转换为从0开始
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
2022-2028 global splicing display industry research and trend analysis report
HTB-Devel
random shuffle注意
Pytest (6) -fixture (Firmware)
Practice of traffic recording and playback in vivo
Gbase 8C system table PG_ constraint


