回文字符串的判断代码

一个可以判断字符串是否回文Pallindrome的c程序

 1 #include <stdio.h>
 2 #include <stdbool.h>
 3 #include <string.h>
 4 
 5 bool isPalindromeNumber(const char *string);
 6 
 7 int main(int argc, const char * argv[])
 8 {
 9 
10     // insert code here...
11     printf("Begin!
");
12     
13     char *string = "abcccbaa";
14     bool ret = isPalindromeNumber(string);
15     
16     if (ret) {
17         printf("string is palindrome number.
");
18     } else {
19         printf("string is not palindrome number.
");
20     }
21 
22     
23     return 0;
24 }
25 
26 bool isPalindromeNumber(const char *string)
27 {
28     bool isPNumber = false;
29     
30     int i;
31     size_t j;
32     
33     i = 0;
34     j = strlen(string) - 1;
35     
36     if (strlen(string) < 3) {
37         return false;
38     }
39     
40     while ( j-i > 1) {
41 
42         if (string[i] == string[j]) {
43             i++;
44             j--;
45         } else {
46             isPNumber = false;
47             break;
48         }
49         
50         if (j - i <= 1) {
51             isPNumber = true;
52         }
53     }
54     
55     return isPNumber;
56 }
判断是否回文的代码 Code

 在这个基础上可以判断一个字符串中最长的回文长度

  1 #include <stdio.h>
  2 #include <stdbool.h>
  3 #include <string.h>
  4 
  5 bool isPalindromeNumber(const char *string);
  6 bool isPalindrome(const char *string, int sp, int ep);
  7 int max(int a, int b);
  8 int getMaxPalindromeLength(const char *string, int sp, int ep);
  9 
 10 int main(int argc, const char * argv[])
 11 {
 12 
 13     // insert code here...
 14     printf("Begin!
");
 15     
 16     char *string = "abcccbfedppdefabcdef";
 17     bool ret01 = isPalindromeNumber(string);
 18     
 19     if (ret01) {
 20         printf("string is palindrome number.
");
 21     } else {
 22         printf("string is not palindrome number.
");
 23     }
 24     
 25     size_t endPoint = strlen(string) - 1;
 26     bool ret02 = isPalindrome(string, 0, (int)endPoint);
 27     if (ret02) {
 28         printf("string is palindrome number.
");
 29     } else {
 30         printf("string is not palindrome number.
");
 31     }
 32     
 33     int length = getMaxPalindromeLength(string, 0, (int)endPoint);
 34     printf("max palindrome length %d
", length);
 35 
 36     return 0;
 37 }
 38 
 39 bool isPalindromeNumber(const char *string)
 40 {
 41     bool isPNumber = false;
 42     
 43     int i;
 44     size_t j;
 45     
 46     if (strlen(string) < 3) {
 47         return false;
 48     }
 49     
 50     i = 0;
 51     j = strlen(string) - 1;
 52     
 53     while ( j-i > 1) {
 54 
 55         if (string[i] == string[j]) {
 56             i++;
 57             j--;
 58         } else {
 59             isPNumber = false;
 60             break;
 61         }
 62         
 63         if (j - i <= 1) {
 64             isPNumber = true;
 65         }
 66     }
 67     
 68     return isPNumber;
 69 }
 70 
 71 bool isPalindrome(const char *string, int sp, int ep)
 72 {
 73     bool ret = false;
 74     
 75     if ( ep + 1 > strlen(string) ) {
 76         return false;
 77     }
 78     if (ep - sp < 2) {
 79         return false;
 80     }
 81     while (ep - sp > 1) {
 82         if (string[sp] == string[ep]) {
 83             sp++;
 84             ep--;
 85         } else {
 86             return false;
 87         }
 88         if (ep - sp <= 1) {
 89             return true;
 90         }
 91     }
 92     return ret;
 93 }
 94 
 95 int getMaxPalindromeLength(const char *string, int sp, int ep)
 96 {
 97     printf("current string : ");
 98     for (int i=sp; i<=ep; i++) {
 99         printf("%c",string[i]);
100     }
101     printf("
");
102     
103     int length = 0;
104     if (ep - sp + 1 < 3) {
105         return 0;
106     }
107     if (isPalindrome(string, sp, ep)) {
108         length = ep - sp + 1;
109     } else {
110         int tmp01 = getMaxPalindromeLength(string, sp + 1, ep);
111         int tmp02 = getMaxPalindromeLength(string, sp, ep - 1);
112         length = max(tmp01, tmp02);
113     }
114     return length;
115 }
116 
117 int max(int a, int b)
118 {
119     return (a >= b)?a:b;
120 }
获取最长回文的代码 Code

还有其他的解决方法,貌似用了动态规划。

原文地址:https://www.cnblogs.com/huangzizhu/p/3730560.html