A palindrome is a string that reads and reads the same text, such as "121", "ABBA", "X", and so on. This example writes a function to determine whether a string is a palindrome.
Introduce two pointer variables, at the beginning, two pointers to the first character of the string, when two pointers to the word typeface, etc., two pointers move backwards and forwards a character position, and continue to compare, until two pointers meet, indicating that the string is a palindrome, if the comparison process found that two pointers to the characters are not equal, Then the string is judged not to be a palindrome.
Here is the implementation part of the code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N -intCycle (Char*s);/** * Palindrome refers to a string of the same reading and anti-read content, such as "121", "ABBA", "X" and so on. * This example writes a function to determine whether a string is a palindrome. * */intMain () {CharS[n]; while(1) {printf ("Please input the string want to judge (input ^ to quit): \ n"); scanf'%s ', &s);if(s[0] ==' ^ '){ Break; }if(Cycle (s)) {printf ("%s is a cycle string!\n", s); }Else{printf ("%s is not a cycle string!\n", s); } }return 0;}/** * Determine if the string s is a palindrome * * param: * Char *s: The string to be judged * return: * 0: Indicates the string S is not a palindrome number * Not 0: Indicates that the string s is a palindrome number */intCycle (Char*s) {Char*h,*t; for(h = s,t = S + strlen (s)-1; t > h;h++,t--)if(*h! = *t) Break;returnT <= H;}
Here is the result of the program running:
In doing this example, let me think of a previous example is to determine whether a number is a palindrome number, is to do so, assuming a number n=232, starting from the numbers, respectively, 2,3,2; these numbers are multiplied by 100,10,1, respectively, to compare and match the original number, will be able to determine whether the number of books is a palindrome number.
C Instance--judging whether a string is a palindrome number