当前位置:网站首页>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
边栏推荐
- Add automatic model generation function to hade
- 二维格式数组格式索引下标连续问题导致 返回json 格式问题
- 《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
- Joking about Domain Driven Design (III) -- Dilemma
- HW initial preparation
- "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
- Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
- Can netstat still play like this?
- [flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
- 【教程】chrome關閉跨域策略cors、samesite,跨域帶上cookie
猜你喜欢
[translation] modern application load balancing with centralized control plane
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
Sqlserver row to column pivot
Kubernetes cluster log and efk architecture log scheme
Add automatic model generation function to hade
How to change the panet layer in yolov5 to bifpn
Xiaodi notes
[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
随机推荐
【翻译】后台项目加入了CNCF孵化器
左值右指解释的比较好的
Gbase 8C system table PG_ database
Random shuffle note
GBase 8c系统表-pg_attribute
处理数据集,使用LabelEncoder将所有id转换为从0开始
Principle and application of database
SQL statement
Gbase 8C system table PG_ aggregate
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
HTB-Devel
怎么将yolov5中的PANet层改为BiFPN
HW initial preparation
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
Simple understanding of SVG
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
HW-初始准备
Monitoring and management of JVM
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition