Naive palindrome substring check written in C.

Below is a quick and naive implementation of a palindrome substring checker written in C. All characters are considered part of the palindrome (i.e. whitespace and punctuation is not filtered out). Substrings of larger palindromes are also printed separately.

 
#include <stdio.h>
#include <string.h>
 
/*
 * printPalindromes - prints all palindromes in 
 *  a string, including substrings of larger 
 *  palindromes. Treats all characters as 
 *  a valid palindrome character, including 
 *  whitespace and punctuation.
 */
void printPalindromes(char *inputString) {
    int center;
    int offset;
    int length = strlen(inputString);
 
    /* odd-sized palindrome is a minimum 3 characters,
     * so center must at least be 1 more than 0, and 
     * must be at least 1 space from the last element.
     *
     * left side: center - offset
     * right side: center + offset
     */
    for(center = 1; center < length - 2; center++) {
      for (offset = 1; 
          center + offset < length 
          && center - offset >= 0
          && inputString[center + offset] == inputString[center - offset];
          offset++) {
        // print subString from center - offset to center + offset
        printf("%.*sn", offset * 2 + 1, &inputString[center - offset]);
      }
    }
    /* even-sized palindrome is a minimum of 2 characters
     * (the center). Center and center + 1 must match for even
     * length
     */
    for(center = 0; center < length - 3; center ++) {
      if(inputString[center] != inputString[center + 1]) {
        continue;
      }
      /* we know this matches by default */
      printf("%.2sn", &inputString[center]);
 
      /* offsets on the right side are one over because "center"
       * is actually 2 characters.
       * left side: center - offset
       * right side: center + offset + 1
       */
      for(offset = 1;
          center + offset + 1 < length
          && center - offset >= 0
          && inputString[center + offset + 1] == inputString[center - offset];
          offset ++) {
        // print subString from center - offset to center + offset + 1
        printf("%.*sn", offset * 2 + 2, &inputString[center - offset]);
      }
    }
}
 
int main(int argc, char **argv) {
  int arg;
 
  for(arg=1; arg < argc; arg++) {
    printPalindromes(argv[arg]);
  }
}

Example results:


CostaRica:c thomaspowell$ ./palindromes "why forerof baad baab ddi"
rer
orero
forerof
 forerof 
aa
aa
baab
 baab 
d baab d