Checking for palindrome string in c

这一生的挚爱 提交于 2019-12-22 10:58:59

问题


I am accepting a string as a command line argument. I want to check whether the inputted string is a palindrome or not and print the result. I have written the following code. But its displaying the result 'not palindrome' for all inputs.

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

int main(int argc, char argv[20]) {
    int i;
    int l = strlen(argv);    
    char str[20];
    bzero(str, 20);

    for(i=0; i<l; i++)
    {
        str[i] = argv[i+2];
    } 
    int flag;
    int len = strlen(str);
    for(i=0; i< len/2; i++)
    {
        if(str[i] == str[len - (i+2)])
        {
            flag = 0;
        }
        else
        {
            flag = 1;
            break;
        }
    }

    if(flag == 0)
        printf("Palindrome\n");
    else
        printf("Not a palindrome\n");
}

回答1:


You could do it in a K&R-style by having two offset iterators in a for-loop:

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

int main(int argc, char *argv[]) {
    assert(argc != 1);

    char *text = argv[1];

    int len = strlen(text);
    int is_palindrome = 1;
    int i, j;

    for(i = 0, j = len - 1; i < len / 2; i++, j--) {
        if(text[i] != text[j]) {
            is_palindrome = 0;
            break;
        }
    }

    printf("%s a palindrome.\n", is_palindrome ? "is" : "isn't");

    return(0);
}

Changes from original:

  • Changed shift(len >> 1) to division(len / 2) as tenfour suggested.



回答2:


Updated based on comments:

int is_palindrome(const char *s)
{
   const char *t = s + strlen(s);
   while (s<t && *s==*--t) s++;
   return s>=t;
}

And since the OP wants a version that's not so heavy on pointers:

int is_palindrome(const char *s)
{
   size_t i=0, j = strlen(s);
   while (i<j && s[i]==s[--j]) i++;
   return i>=j;
}

For reference, here's the original buggy version:

int is_palindrome(const char *s)
{
   const char *t = s + strlen(s) - 1;
   while (s<t && *s++==*t--);
   return s>=t;
}



回答3:


For one thing, your signature for main is off. It should be int main(int argc, char** argv) or int main(int argc, char * argv[]). You're treating a pointer to a string as if it were a string.

When you've changed that, the string you want should be in argv[1] (since argv[0] is some representation of the program name).




回答4:


There's a good case for using pointers rather than indexes for this:

int is_palindrome(const char *s) {
    const char *end = s + strlen(s);
    while (end > s) {
        --end;
        if (*end != *s) return 0;
        ++s;
    }
    return 1;
}

If you like short, confusing code, you can re-write that:

int is_palindrome(const char *s) {
    const char *end = s + strlen(s);
    while (end > s) if (*(--end) != *(s++)) return 0;
    return 1;
}

argv isn't a string, it's an array of strings, one for the program name and then one for each argument (usually space-separated in a command line). So to test if the first argument is a palindrome, you're interested in argv[1].

int main(int argc, char **argv) {
    if (argc != 2) {
        printf("usage: %s <string>\n", argv[0]); // or something
        return 1;
    }
    if (is_palindrome(argv[1])) {
        printf("Palindrome\n");
    } else {
        printf("Not a Palindrome\n");
    }
}



回答5:


The first loop doesn't make sense. Copying the string to another doesn't make sense.

Just do it and adjust the index:

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

int main(int argc, char **argv) {
int i;
char * str = argv[1];
int flag;

int len = strlen(str);

for(i=0; i< (len+1)/2; i++)
{

    printf("DEBUG: Comparing %c %c\n",str[i], str[len - (i+1)]);


    if(str[i] == str[len - (i+1)])
    {
        flag = 0;
    }
    else
    {
        flag = 1;
        break;
    }
}

    if(flag == 0)
    printf("Palindrome\n");
else
    printf("Not a palindrome\n");
}



回答6:


No pointers (except the one use for making a copy of the original string).

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

int main( int argc, char *argv[] )
{
    char    *s2;

    if ( argc != 2 )
        return ( 1 );   //  not properly invoked

    if ( (s2 = strdup( argv[1] )) == NULL )
        return ( 2 );   //  failed (not likely)

    printf( "\"%s\" %s a palindrome.\n", argv[1], strcmp( argv[1], strrev( s2 ) ) ? "is not" : "is" );

    free( s2 );

    return ( 0 );
}


来源:https://stackoverflow.com/questions/3471076/checking-for-palindrome-string-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!