/* Include lib */
#include <stdio.h>
#include <assert.h>
#include <string.h>
/* Define */
#define STR_MAX 4
char str[STR_MAX] = "aaa"; /* Size = Number of chars + 1 for \0 */
/* API */
/* API check palindromic string */
int check_palindromic(char *str, int size)
{
int i;
if (!str) {
printf("NULL input\n");
return -1;
}
if (size == 0)
return 1;
for (i = 0; i < (size / 2); i++) {
if (str[i] != str[size - 1 - i])
return 1;
}
return 0;
}
/* Main func interract with user */
int main()
{
char tmp[STR_MAX] = { 0 };
int i, j, cnt, last = STR_MAX - 2;
/* Find all palindromic substrings */
/*
* Algo:
* - Loop each case number of substring: 1 -> 2 -> ... -> max chars.
* - In each case, loop to find all posible substring. Check palindromic.
* - Store number appearance if matching requirement.
*/
cnt = 0;
for (i = 1; i < STR_MAX; i++) {
/* Find sub-string i chars: start from first char to which char had enough nextto chars for that size */
for (j = 0; j <= (last - (i - 1)); j++) {
if (check_palindromic(&str[j], i) == 0) {
memset(tmp, 0, STR_MAX);
memcpy(tmp, &str[j], i);
printf("%d: %s\n", cnt, tmp);
cnt++;
}
}
}
printf("=> %d\n", cnt);
}
#if 0 /* Sample code */
int find_all_palindrome_substrings(string & input) {
//TODO: Write - Your - Code
return -1;
}
#endif
To embed this project on your website, copy the following code and paste it into your website's HTML: