TransWikia.com

Palindrome Checker for strings in C

Stack Overflow Asked by Bradley Felix on January 16, 2021

I’m a new CS student and I was having trouble with my palindrome function. It works with single words perfectly fine and it also works with some multi-words, such as "race car", though not all. For my assignment, one of the words that I need to check is "dot i tod", though I can’t get the palindrome to work properly. I would appreciate any help! (Assume only one space between words and all words are lowercase)

EDIT: I realized that I should’ve put the beginning of my function, so I’ll put that in now.

char sentence[20] = "dot i tod";
int len = strlen(sentence);
int i = 0;
int end = len;
char left[i];
char right[end];
 while (i < len){
   printf("%c ", sentence[i]);
      printf("n");
      printf("%c ", sentence[len - i - 1]);
      printf("n");
   if (left[i] == right[end - i  - 1]){
     i++;
     end--;
   }
   if (left[i] == ' '){
     i++;
   }
   if (right[end] == ' '){
     end--;
   }
   if (left[i] != right[end]){
     printf("Not a palindrome");
     break;
   }
   if (i >= end){
     printf("Palindrome");
     break;
   }
 }
}

One Answer

For starters these declarations

char left[i];
char right[end];

are incorrect and do not make sense. For example you may not declare a variable length array with the size equal to 0.

But if instead of the variables left and right (that is instead of uninitialized arrays) to use the variable sentence then nevertheless the logic of your program is incorrect.

Let's assume that the string contains only one character for example 'A'.

In this case the condition in the if statement evaluates to logical true

if (sentence[i] == sentence[end - i - 1]){ i++; end--; }

As a result after the increment of i it will point to the terminating zero ''. While after the decrement of end it will point to the first and single character 'A'.

After that this if statement will evaluate also to the logical true

if (sentence[i] != sentence[end]){ printf("Not a palindrome"); break; }

because now And the program outputs that the string is not a palindrome.

Always try to write a more generic function.

For example the function can look the following way.

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

int is_palindrome( const char *s )
{
    const char *first = s, *last = s + strlen( s );
    int success = 1;
    
    do
    {
        while ( first != last && isspace( ( unsigned char )*first ) ) ++first;
        
        while ( last != first && isspace( ( unsigned char )*--last ) );

        if ( ( success = first != last && *first == *last ) ) ++first;
    } while ( success && first != last );
    
    return first == last;
}

int main(void) 
{
    const char *s1 = "race car";
    const char *s2 =  "dot i tod";
    const char *s3 = " 1 2 3 2 1 ";
    const char *s4 = " 1 2 33 2 1 ";
    const char *s5 = " 1 2 34 2 1 ";
    
    return 0;
}

The program output is

"race car" is palindrome
"dot i tod" is palindrome
" 1 2 3 2 1 " is palindrome
" 1 2 33 2 1 " is palindrome
" 1 2 34 2 1 " is not palindrome

Answered by Vlad from Moscow on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP