简体   繁体   中英

Why does my palindrome function always return 1?

I'm writing a function for my homework which is supposed to tell if a given string is a palindrome or not. Although I even tried it on paper with the word "otto", my program always returns 1. Although this is a quite common question, I'd really like to know what I'm doing wrong instead of just copying a solution from here.

int is_palindrom(const char* palin)
{
    int size = strlen(palin), i=0;
    for (i=0;i<=(size/2); ++i)
    {
        if(palin[i] != palin[(size - i -1)])
        {
            return 1;
        }
    }
    return 0;
}   

Your code is correct, however please note that you may have an inverted logical expression. You are returning 1 in case of not equal, and 0 when it is. This means your function is working the opposite of "standard" C functions, where 1 evaluates to true .

Obviously, you are free to use whichever value you like to represent whatever you want. However, this can easily lead to confusion if someone else is reading your code. If bool is available, you should be using that; otherwise, you should always assume 1 is true and 0 is false.

Also, make sure to note is_palindrome takes a string and not an integer.

ie you must call it as is_palindrome("767") and not is_palindrome(767)

Your code does return 0 when it should. I am guessing when you read the string you pass as argument to your function, there are extra characters appended to the string, most probably a new line character. Try debugging the application or adding debug output in the function. For instance print the length of the string and the ascii codes of the characters in it.

Here is the code I used to verify it:

#include <stdio.h>
#include <string.h>
int is_palindrom(const char* palin)
{
  int size = strlen(palin), i=0;
  for (i=0;i<=(size/2); ++i)
  {
    if(palin[i] != palin[(size - i -1)])
    {
      return 1;
    }
  }
  return 0;
} 

int main(void) {
  printf("%d", is_palindrom("otto"));
  return 0;
}

调用此函数时,请确保您的(const char *)结尾处带有“ \\ 0”。

#include<stdio.h>
#include<conio.h>

int is_palindrom(const char* jj);

int main(char *args){

         int rr =   is_palindrom("otto");
         printf("rsult is %d", rr);
         getch();

}

int is_palindrom(const char* palin) 
    { 
        int size = strlen(palin), i=0; 
        for (i=0;i<=(size/2); ++i) 
        { 
            if(palin[i] != palin[(size - i -1)]) 
            { 
                return 1; 
            } 
        } 
        return 0; 
    }

I ran you code using above code snippet and it work fine for me.it returns 0 if palindrome is entered and 1 if entered value is not palindrome. the main part of the function is the loop for (i=0;i<=(size/2); ++i) and the comparison if(palin[i] != palin[(size - i -1)]) the loop starts from 0 and then in condition palin[0] element and palin[4-0-1] ie palin[3] element first o and last o in this case are mapped then the increement ++i takes place and then nest mapping of palin[second] and palin[second-last] elements happen so you can you either `++i' or 'i++'

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