简体   繁体   English

回文 function,不在 C 中返回值

[英]Palindrome function, not returning value in C

When I run this function I don't get a return value of 1 or 0. Im not sure why, I'm new to pointers and any type of help/tips would be greatly appreciated.当我运行这个 function 时,我没有得到 1 或 0 的返回值。我不知道为什么,我是指针新手,任何类型的帮助/提示都将不胜感激。

int isPalindrome (char * str)
{
    char def[SIZE];
    int length = strlen(str);
    for(int count; count <= length; count++ ){
        def[count] = str[count];
    }

    int c;
    char *begin, *end, temp;

    begin  = str;
    end    = str;

    for (c = 0; c < length - 1; c++)
        end++;

    for (c = 0; c < length/2; c++)
    {        
        temp   = *end;
        *end   = *begin;
        *begin = temp;

        begin++;
        end--;
    }

    for(int count2; count2 <= length; count2++){
        if(str[count2] != def[count2]){
            return 0;
        }
        return 1;
    }
}

The function is called with.. function 被称为..

 if(isPalindrome(arr) == 1) 
     printf ("\nIs a palindrome.\n\n");

To check if a string is a palindrome you create a reversed string, after first allocating enough room for all chars, by looping through the original one and changing the order.要检查一个字符串是否是回文,您可以创建一个反转字符串,首先为所有字符分配足够的空间,然后循环遍历原始字符串并更改顺序。 you can then use a strcmp to check if the reversed and original string are the same.然后,您可以使用 strcmp 检查反向字符串和原始字符串是否相同。

int isPalindrome (char * str)
{
  int length = strlen(str); 
  char* reversed = malloc(sizeof(char)*length); //we allocate enough space for length chars

  for(int i = 0; i < length; i++) {
    reversed[i] = str[length-1-i]; //populate reversed with the chars in str but in the reversed order
  }

  if(strcmp(str,reversed) == 0) //strcmo(a,b) return 0 if they are equal
  {
    free(reversed); //deallocate the space for reversed
    return 1;
  }
  free(reversed); //deallocate the space for reversed
  return 0;
}

In these loops在这些循环中

for(int count; count <= length; count++ ){
    def[count] = str[count];
}

and

for(int count2; count2 <= length; count2++){
    if(str[count2] != def[count2]){
        return 0;
    }
    return 1;
}

there are used uninitialized variables count and count2 .使用了未初始化的变量countcount2 So the function has undefined behavior.所以 function 有未定义的行为。

Pay attention the the function is declared and defined incorrectly.注意 function 的声明和定义不正确。 It is too complicated and uses a magic number SIZE .它太复杂了,并且使用了一个幻数SIZE

Also you should use the type size_t instead of the type int because the return type of the function strlen is size_t and in general an object of the type int can not accommodate an object of the type size_t .此外,您应该使用size_t类型而不是 int 类型,因为 function strlen的返回类型是size_t并且通常int类型的 object 不能容纳size_t类型的 ZA8CFDE6331BD59EB2AC96F89111C4。

There is no need to create an auxiliary array and change the original string to check whether the given string is a palindrome.无需创建辅助数组并更改原始字符串以检查给定字符串是否为回文。 Moreover the parameter shall be defined with the qualifier const .此外,参数应使用限定符const定义。 Otherwise you will be unable to check whether a string literal is a palindrome because modifying a string literal invokes undefined behavior.否则,您将无法检查字符串文字是否是回文,因为修改字符串文字会调用未定义的行为。

Also if you are trying to use pointers then there is no need to use also indices in loops.此外,如果您尝试使用指针,则无需在循环中也使用索引。

The function can be defined much simpler. function 的定义要简单得多。

Here you are.给你。

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

_Bool isPalindrome( const char *s )
{
    const char *first = s, *last = s + strlen( s );

    if ( first != last )
    {
        while ( first < --last && *first == *last ) ++first;
    }

    return !( first < last );
}

int main(void) 
{
    char *s1 = "121";

    printf ( "\"%s\" is %s%s\n", s1, isPalindrome( s1 ) ? "a " : "not ", "palindrome." );

    char *s2 = "1221";

    printf ( "\"%s\" is %s%s\n", s2, isPalindrome( s2 ) ? "a " : "not ", "palindrome." );

    return 0;
}

The program output is程序 output 是

"121" is a palindrome.
"1221" is a palindrome.

As you can see the function uses only pointers and neither index.如您所见,function 仅使用指针而不使用索引。

Your program has multiple problems.您的程序有多个问题。 Lets see them one by one:让我们一一看看:

for(int count; count <= length; count++ ){
            ^^^^
    def[count] = str[count];
}

You are not initialising count, you are just declaring it.您没有初始化计数,您只是在声明它。 Without any initial value count will just be some garbage.没有任何初始值计数将只是一些垃圾。 To fix this:要解决这个问题:

for(int count = 0; count <= length; count++ ){
    def[count] = str[count];
}

Please note that older C versions require you to declare all variables in the beginning of a function block as far as I remember.请注意,据我所知,较旧的 C 版本要求您在 function 块的开头声明所有变量。 So this is anyways wrong.所以这无论如何都是错误的。

Next, your palindrome logic is incorrect.接下来,您的回文逻辑不正确。 You are just swapping characters between begin and end.您只是在开始和结束之间交换字符。

All you have to do is check while iterating HALF THE STRING is whether the characters from the beginning equal to the end.您所要做的就是在迭代 HALF THE STRING 时检查开头的字符是否等于结尾。

for (c = 0; c < length/2; c++)
{
    // Check if "begin" equals to the current "end"
    if (*begin != *end) 
    {
        return 0;
    }

    // Move "begin" forward and "end" backwards
    begin++;
    end--;
}

Here is your complete function in working condition:这是您在工作状态下的完整 function:

int isPalindrome(char* str)
{
    int length = strlen(str);

    int c;
    char *begin, *end;

    begin = str;

    // Why use the below for loop when you can directly move end to the end?
    end = str + length - 1;

    // for (c = 0; c < length - 1; c++)
    //    end++;

    for (c = 0; c < length / 2; c++) {
        if (*begin != *end) {
            return 0;
        }

        begin++;
        end--;
    }
    return 1;
}

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

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