A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Longest Palindromic Substring in C page! Here, you'll find the source code for this program as well as a description of how the program works.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LENGTH 1000
int expandAroundCenter(const char* s, int left, int right) {
while (left >= 0 && right < strlen(s) && s[left] == s[right]) {
left--;
right++;
}
return right - left - 1;
}
bool longestPalindromicSubstring(const char* s, char* result) {
if (s == NULL || strlen(s) == 0) {
strcpy(result, "");
return false;
}
int start = 0, maxLength = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = (len1 > len2) ? len1 : len2;
if (len > maxLength) {
start = i - (len - 1) / 2;
maxLength = len;
}
}
if (maxLength > 1) {
strncpy(result, s + start, maxLength);
result[maxLength] = '\0';
return true;
}
strcpy(result, "");
return false;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: please provide a string that contains at least one palindrome\n");
return 1;
}
const char* input = argv[1];
char result[MAX_LENGTH];
if (longestPalindromicSubstring(input, result) && strlen(result) > 0) {
printf("%s\n", result);
} else {
printf("Usage: please provide a string that contains at least one palindrome\n");
}
return 0;
}
Longest Palindromic Substring in C was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.