@fernandozamoraj/C isPalindrome

No description

  • main.c
#include "stdio.h"
#include <stdlib.h>
#include <string.h>

  Author: Fernando Zamora
  a palindrome is a word or phrase that reads the same forward or backwards.
  This code checks for palindromes.
  Challenge for you!!!
  1. Fix the code so that spaces are ignored (i.e. 'race car' should return true for palindrome)
  2. Fix the code so that case is ignored   (i.e. 'Racecar' should return true for palindrome)

int getLength(const char* source){
  int length = 0;
  while(source[length] != '\0'){
  return length;

char* reverse(char* target, const char* source){
  int length = getLength(source);
  for(int i = 0; i < length; i++){
    target[length-(1+i)] = source[i];
    if(length == i)
  target[length] = '\0';
  return target;

int isPalindrome(const char* source){
  char* target = malloc(sizeof(source[0]) * (getLength(source)+1));
  if( strcmp(reverse(target, source), source) == 0 ){
    return 1;
  return 0;

int main(void) {
  char target[100] = {0};
  printf("\n'foo' = '%s'", reverse(target, "foo"));
  printf("\n'racecar' = '%s'", reverse(target, "racecar"));
  printf("\n'fernando' = '%s'", reverse(target, "fernando"));

  printf("\n'racecar' is palindrome: %d", isPalindrome("racecar"));
  printf("\n'adam' is palindrome: %d", isPalindrome("adam"));
  printf("\n'amma' is palindrome: %d", isPalindrome("amma"));
  printf("\n'Amma' should be a palindrome: %d", isPalindrome("Amma"));
  return 0;
gcc version 4.6.3