0

I'm trying to make a palindrome finder in C and I don't know where it is going wrong, no matter what I get the output false on the 2 different ways that I have tried to code this. I have only just started C (in the past week) so if you could explain things simply that'd be great, thanks!

//way1
#include <stdio.h>

int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }

void print_char(int c)     { putchar(c); }   
void print_string(char* s) { printf("%s", s); }


int is_palin(char word[]) {

  int m = 0;
  int arr_len = sizeof(word) / sizeof(char); //change to char_index
  int n = arr_len;
  int t = 1;

  if(n % 2 != 0) {
    for (m=0; m < ((n-1)/2); m++) {
      if(word[m] != word[n-m-2]) {
        t = 0;
      }
      else {
        t = 1;
      }
    }
  }
  else {
    for (m=0; m < (n/2)-1; m++) {
      if(word[m] != word[n-m-2]) {
        t = 0;
      }
      else {
        t = 1;
      }
    }
  }

  if(t == 1) {
    return 1;
  }
  else {
    return 0;
  }
}

int main(void) {
  char word[6] = "civic";
  int arr_len = sizeof(word)/sizeof(char);

  if (is_palin(word) == 1) {
    printf("is palin\n");
  }
  else {
    printf("is not palin\n");
  }

  printf(word);
  printf("\n");
  printf("%d\n", arr_len);
  return 0;
}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

//way2
#include <stdio.h>

int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }

void print_char(int c)     { putchar(c); }   
void print_string(char* s) { printf("%s", s); }


int is_palin(char word[]) {
  int m = 1;
  int input_length = sizeof(word);
  int j = input_length-1;
  int i = 0;

  for(i=0; i <= j; i++) {
    if(word[i] != word[j]) {
      m = 0;
      j--;
    }
  }

  if(m == 1) {
    return 1;
  }
  else {
    return 0;
  }
}


int main(void) {
  char word[6] = "civic";
  int input_length = sizeof(word);

  if (is_palin(word) == 1) {
    printf("is palin\n");
  }
  else {
    printf("is not palin\n");
  }

  printf(word);
  printf("\n");
  printf("%d\n", input_length);
  return 0;
}
  • 5
    `sizeof word` does not result in what you expect. You actually need strlen() here. – wildplasser Oct 25 '15 at 21:52
  • ..something you would have found yourself, very quickly, if you had used a debugger. 'I have only just started C (in the past week)' OK, but you need to learn how to debug NOW before you write any more code. – Martin James Oct 25 '15 at 22:05
  • ... but putting a few printf() statements at strategic points can be just as effective as *using a debugger*. BTW: fgets() reads the input *including the final '\n'* You probably want to remove that first, before trying to find (no) palindromes. – wildplasser Oct 25 '15 at 22:18
  • I would advice you that this is not a good idea to declare function like that. Before main you should only write prototypes of functions, after main you actually have to declare the function. – Claudio Cortese Jan 14 '16 at 13:30

2 Answers2

0

Please try this, it works fine.

   #include <stdio.h>

    int main(  )
    {
      int flag = 0;
      int length = 0;
      int len2 = 0;
      int i = 0;
      char name[130];
      char p[130];
      char q[130];

      printf( "please enter a name or sentence\n" );
      scanf( "%[^\n]", name );

      length = strlen( name );
      len2 = length;
      strcpy( p, name );
      memset( q, '.', length ); // handy to debug comparaison
      q[length] = '\0';

      for ( i = 0; i < length; i++ )
      {
        q[--len2] = p[i];
      }

      printf( "\n p==%s", p );
      printf( "\n q==%s", q );
      getchar(  );

      if ( !strcmp( p, q ) )
        flag = 1;

      if ( flag == 1 )
        printf( "\npalindrome\n" );
      else
        printf( "\nnot a palindrome\n" );

      return 0;
    }
BobRun
  • 756
  • 5
  • 12
0

Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work):

for(i = 0; i < string_length; i++)
    {
            if(sentence[i] == sentence[string_lenght-1-i])
                    palindrome = true;
            else
            {
                    palindrome = false;
                    break;
            }
    }

Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. You can use something like

if(palindrome)
     printf(..);
else
     printf(..);

for a simple prompt for the user.

Example :

radar is palindrome

abba is palindrome

abcabc is not palindrome

Please , pay attention to the fact that

Abba

is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes :

'A' has the value of 65

'a' has the value of 97 according to the ASCII table. You can find out more here.

You can avoid this issue trasforming all the characters of the string to lower case characters. You can do this including the <ctype.h> library and calling the function int tolower(int c); like that :

for ( ; *p; ++p) *p = tolower(*p);

or

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

Code by Earlz, take a look at this Q&A to look deeper into that.

EDIT : I made a simple program to do this, see if it can help you

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

void LowerCharacters(char *word, int word_lenth);

int main(void){

    char *word = (char *) malloc(10);
    bool palindrome = false;

    if(word == 0)
    {
        printf("\nERROR : Out of memory.\n\n");
        return 1;
    }

    printf("\nEnter a word to check if it is palindrome or not : ");
    scanf("%s", word);

    int word_length = strlen(word);

    LowerCharacters(word,word_length);

    for(int i = 0; i < word_length; i++)
    {
        if(word[i] == word[word_length-1-i])
            palindrome = true;
        else
        {
            palindrome = false;
            break;
        }
    }

    palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);

    free(word);

return 0;

}

void LowerCharacters(char *word, int word_length){

    for(int i = 0; i < word_length; i++)
        word[i] = tolower(word[i]);
}

Input :

Enter a word to check if it is palindrome or not : RadaR

Output :

The word radar is palindrome.

Community
  • 1
  • 1
Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21