#include #include #include #include #include #include #include int longestPalSubstr(char *str) { int maxLength = 1; int start = 0; int len = strlen(str); int low, high; for (int i = 1; i < len; ++i) { low = i - 1; high = i; while (low >= 0 && high < len && str[low] == str[high]) { if (high - low + 1 > maxLength) { start = low; maxLength = high - low + 1; } --low; ++high; } low = i - 1; high = i + 1; while (low >= 0 && high < len && str[low] == str[high]) { if (high - low + 1 > maxLength) { start = low; maxLength = high - low + 1; } --low; ++high; } } printf("Longest palindrome substring is: "); printSubStr(str, start, start + maxLength - 1); return maxLength; } int main() { char str[] = "week"; printf("nLength is: %dn", longestPalSubstr( str ) ); return 0; }