当前位置:网站首页>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
边栏推荐
- Super easy to use logzero
- [translation] flux is safe. Gain more confidence through fuzzy processing
- Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
- Cancer biopsy instruments and kits - market status and future development trends
- 处理数据集,使用LabelEncoder将所有id转换为从0开始
- Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
- GBase 8c系统表pg_cast
- Apple releases MacOS 11.6.4 update: mainly security fixes
- Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
- Gbase 8C system table PG_ amop
猜你喜欢
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
Matlab tips (24) RBF, GRNN, PNN neural network
【翻译】后台项目加入了CNCF孵化器
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
Add automatic model generation function to hade
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
Basic operation of binary tree (C language version)
随机推荐
GBase 8c系统表-pg_attribute
javeScript 0.1 + 0.2 == 0.3的问题
Practice of traffic recording and playback in vivo
Kubernetes cluster log and efk architecture log scheme
How to change the panet layer in yolov5 to bifpn
Add automatic model generation function to hade
定了,就选它
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
2022-2028 global splicing display industry research and trend analysis report
Process the dataset and use labelencoder to convert all IDs to start from 0
Use cve-2021-43893 to delete files on the domain controller
sql server 查詢指定錶的錶結構
基于can总线的A2L文件解析(2)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
Pytest (6) -fixture (Firmware)
Classes and objects - initialization and cleanup of objects - constructor call rules
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
[translation] modern application load balancing with centralized control plane
"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
为什么会选择框架?选择什么样的框架