简体   繁体   English

使用指针检查字符串是否为回文

[英]Checking a string if it is palindrome or not using pointer

What does the bold part stand for in this code?这段代码中粗体部分代表什么? I've managed to fulfill most of the template (given by my teacher as assignment), can't find the reason behind that bold part.我已经设法完成了大部分模板(由我的老师作为作业给出),但找不到那个大胆部分背后的原因。

I could use the strlen part before it appeared, I know one can still write the code without strlen at all.我可以在 strlen 部分出现之前使用它,我知道人们仍然可以在没有 strlen 的情况下编写代码。

#include <stdio.h>

int strlen(char *str)
{
    int c = 0;
    while (str[c] != '\0')
    {
        c++;
    }
    return c;
}

int isPalindrome(char *str)
{
    char *ptr1 = str;
    char *ptr2 = str + strlen(str) - 1;
    while (ptr2 > ptr1)
    {
        if (tolower(*ptr1) != tolower(*ptr2))
        {
            return (0);
        }
        ptr1++;

        ptr2--;
    }
    return (1);
}
/**
            This function will
                -return 1 if the given string is Palindrome,
                -return 0 if the given string is not Palindrome.
        */

**int n = strlen(str);
// Write your code here**
}

int main()
{
    char *str = "ABCCBA ABCCBA"; //your input
    if (isPalindrome(str))
    {
        printf("%s is a palindrome", str);
    }
    else
    {
        printf("%s is not a palindrome", str);
    }
}

The teacher hinted that using the length of the zero-terminated input string will be needed.老师暗示将需要使用以零结尾的输入字符串的长度。 They stored that useful value inside a variable.他们将这个有用的值存储在一个变量中。

If you are required to use the code provided by the teacher, then move all your code AFTER that line and within your code replace the strlen(str) with n .如果您需要使用老师提供的代码,请在该行之后移动所有代码,并在代码中将strlen(str)替换为n

int isPalindrome(char *str)
{
        /**
            This function will
                -return 1 if the given string is Palindrome,
                -return 0 if the given string is not Palindrome.
        */

    int n = strlen(str);
    // Write your code here

    char *ptr1 = str;
    char *ptr2 = str + n - 1;
    while (ptr2 > ptr1)
    {
        if (tolower(*ptr1) != tolower(*ptr2))
        {
            return (0);
        }
        ptr1++;

        ptr2--;
    }
    return (1);
}

I think that the function template starts from the declaration of the variable n that gets the size of the passed string.我认为函数模板从获取传递字符串大小的变量n的声明开始。

int isPalindrome(char *str)
{
    /**
        This function will
            -return 1 if the given string is Palindrome,
            -return 0 if the given string is not Palindrome.
    */
    int n = strlen(str);
    // Write your code here*
}

But you ignored this declaration and used the expression strlen( str ) in this declaration但是你忽略了这个声明并在这个声明中使用了表达式strlen( str )

char *ptr2 = str + strlen(str) - 1;

instead of writing for example而不是写作例如

int n = strlen(str);
//...
char *ptr2 = str + n - 1;

In any case the assignment looks not very well.无论如何,分配看起来不太好。 For example this declaration例如这个声明

char *ptr2 = str + strlen(str) - 1;

can invoke undefined behavior when the passed string is empty.当传递的字符串为空时,可以调用未定义的行为。

The functions' parameters should be declared with the qualifier const because passed strings as arguments are not changed in the functions.函数的参数应该用限定符const声明,因为作为参数传递的字符串在函数中不会改变。

The argument of the standard function tolower must be converted to the type unsigned char .标准函数tolower的参数必须转换为unsigned char类型。

The functions could look the following way功能可能如下所示

size_t strlen( const char *str )
{
    size_t n = 0;

    while ( str[n] ) ++n;

    return n;
}

int isPalindrome( const char *str )
{
    const char *ptr1 = str;
    const char *ptr2 = str + strlen( str );

    if ( ptr1 != ptr2 )
    {
        while ( ptr1 < --ptr2 && 
                tolower( ( unsigned char )*ptr1 ) == tolower( ( unsigned char )*ptr2 ) ) 
        {
            ++ptr1;
        }           
    }

    return !( ptr1 < ptr2 );
}

Also if the function strlen also must use pointers then it can look the following way此外,如果函数 strlen 也必须使用指针,那么它可以如下所示

size_t strlen( const char *str )
{
    const char *p = str;

    while ( *p ) ++p;

    return p - str;
}

Here is a demonstration how your functions should look.这是您的函数的外观演示。

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

size_t strlen( const char *str )
{
    const char *p = str;

    while ( *p ) ++p;

    return p - str;
}

int isPalindrome( const char *str )
{
    const char *ptr1 = str;
    const char *ptr2 = str + strlen( str );

    if ( ptr1 != ptr2 )
    {
        while ( ptr1 < --ptr2 && 
                tolower( ( unsigned char )*ptr1 ) == tolower( ( unsigned char )*ptr2 ) ) 
        {
            ++ptr1;
        }           
    }

    return !( ptr1 < ptr2 );
}


int main(void) 
{
    char *str = "ABCCBA ABCCBA"; //your input

    if ( isPalindrome( str ) )
    {
        printf("\"%s\" is a palindrome\n", str);
    }
    else
    {
        printf("\"%s\" is not a palindrome\n", str);
    }   

    return 0;
}

The program output is程序输出是

"ABCCBA ABCCBA" is a palindrome

You can implement isPalindrome without any knowledge about pointers.您可以在不了解指针的情况下实现isPalindrome I prefer to use indices instead of pointers, look at the next example我更喜欢使用索引而不是指针,看下一个例子

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

int strLen(const char *str)
{
    int n = 0;
    while(str[n]) { ++n; }
    return n;
}

int isPalindrome(const char *str) {
    int i = 0;
    int j = strLen(str) - 1;
    while (i < j) {
        if (str[i++] != str[j--]) { return 0; }
    }
    return 1;    
}

int main() {
    assert(isPalindrome(""));
    assert(isPalindrome("a"));
    assert(isPalindrome("aa"));
    assert(isPalindrome("aba"));
    assert(!isPalindrome("ab"));
    assert(!isPalindrome("aab"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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