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

问题描述 投票:1回答:3

此代码中的粗体部分代表什么?我已经设法完成了大部分模板(由我的老师分配给作业),找不到那个粗体部分背后的原因。

我可以在出现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);
    }
}

c palindrome
3个回答
0
投票

老师提示需要使用以零结尾的输入字符串的长度。他们将有用的值存储在变量中。

如果需要使用老师提供的代码,请在该行之后移动所有代码,并在代码内将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);
}

0
投票

您可以在不了解指针的情况下实现isPalindrome。我更喜欢使用索引而不是指针,请看下一个示例

#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"));
}

0
投票

我认为函数模板从此声明开始

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

但是您忽略了此声明,并在此声明中使用了表达式strlen( str )

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

无论如何,分配看起来都不是很好。例如,此声明

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

当传递的字符串为空时,可以调用未定义的行为。

函数的参数应使用限定符const声明,因为在函数中传递的字符串作为参数不会更改。

这些函数的外观如下:>

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 && *ptr1 == *ptr2 ) ++ptr1;
    }

    return !( ptr1 < ptr2 );
}
© www.soinside.com 2019 - 2024. All rights reserved.