简体   繁体   中英

Palindrome finder in C?

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;
}

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;
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM