C Instance--judging whether a string is a palindrome number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.