#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0

    Program to determine palindrome.


    I'm trying to write a program to determine if a string is a palindrome. This is what I have so far, but I keep getting errors on malloc when I try to compile. Can someone point out what I did wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    int is_palindrome(char*);
    void copy_string(char*, char*);
    void reverse_string(char*);
    int string_length(char*);
    int compare_string(char*, char*);
    int main(void)
    {
      char string[100];
      int result;
    
      printf("Enter a phrase to check if it is a palindrome.\n");
      fgets(string, 101, stdin);
    
      result = is_palindrome(string);
    
      if ( result == 1 )
        printf("\"%s\" isa palindrome!\n", string);
      else
        printf("\"%s\" is not a palindrome.\n", string);
    
      return 0;
    }
    
    int is_palindrome(char *string)
    {
      int check, length;
      char *reverse;
    
      length = string_length(string);
    
      reverse = (char*) malloc (length+1);
    
      copy_string(reverse, string);
      reverse_string(reverse);
    
      check = compare_string(string, reverse);
    
      free (reverse);
    
      if ( check == 0 )
        return 1;
     else
        return 0;
    }
    
    int string_length(char *string)
    {
      int length = 0;
    
      while(*string)
      {
        length++;
        string++;
      }
    
      return length;
    }
    
    void copy_string(char *target, char *source)
    {
      while(*source)
      {
        *target = *source;
        source++;
        target++;
      }
      *target = '\0';
    }
    
    void reverse_string(char *string)
    {
      int length, c;
      char *begin, *end, temp;
    
      length = string_length(string);
    
      begin = string;
      end = string;
    
      for ( c = 0; c < ( length - 1 ); c++ )
        end++;
      for ( c = 0; c < length/2; c++ )
      {
        temp = *end;
    *end = *begin;
        *begin = temp;
    
        begin++;
        end--;
      }
    }
    
    int compare_string(char *first, char *second)
    {
      while(*first==*second)
      {
        if ( *first == '\0' && *second == '\0' )
          return 0;
        else
          return -1;
    }
    }
    I also need to make sure and eliminate anything besides letters and convert upper case to lower case and I'm not sure what will be the best point at which to do that in the program. I was thinking before reversing the string?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > This is what I have so far, but I keep getting errors on malloc when I try to compile.
    Well malloc is prototyped in stdlib.h, which isn't included.

    > char string[100];
    >...
    > fgets(string, 101, stdin);
    If you're going to do this properly, use
    fgets(string, sizeof(string), stdin);

    What tests have you done so far?
    Eg.
    Code:
    int main(void)
    {
      char string[100], copy[100];
      int result;
    
      printf("Enter a phrase to check if it is a palindrome.\n");
      fgets(string, sizeof(string), stdin);
    
      printf("String Length=%d\n", string_length(string));
      copy_string(copy,string);
      printf("Original=>>%s<<, copy=>>%s<<\n", string, copy);
      printf("Comparison=%d\n", compare_string(string,copy));
    If these do not work as expected, then you need to fix them before becoming overly worried about the palindrome.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo