回文函数,不返回 C 中的值



当我运行这个函数时,我没有得到 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;
}
}

调用该函数时:

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

要检查字符串是否是回文,您需要在首先为所有字符分配足够的空间后,通过循环遍历原始字符并更改顺序来创建反向字符串。 然后,您可以使用 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;
}

在这些循环中

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

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

有未初始化的变量countcount2。因此,该函数具有未定义的行为。

请注意,函数的声明和定义不正确。它太复杂了,并且使用了一个魔术数字SIZE

此外,您应该使用类型size_t而不是 int 类型,因为函数strlen的返回类型是size_t的,并且通常int类型的对象不能容纳size_t类型的对象。

无需创建辅助数组并更改原始字符串来检查给定字符串是否为回文。此外,参数应使用限定符const定义。否则,您将无法检查字符串文本是否为回文,因为修改字符串文本会调用未定义的行为。

此外,如果您尝试使用指针,则无需在循环中使用索引。

该函数的定义可以简单得多。

给你。

#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%sn", s1, isPalindrome( s1 ) ? "a " : "not ", "palindrome." );
char *s2 = "1221";
printf ( ""%s" is %s%sn", s2, isPalindrome( s2 ) ? "a " : "not ", "palindrome." );
return 0;
}

程序输出为

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

如您所见,该函数仅使用指针,而不使用索引。

您的程序有多个问题。让我们一一看:

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

你不是在初始化计数,你只是在声明它。没有任何初始值计数将只是一些垃圾。要解决此问题,请执行以下操作:

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

请注意,据我所知,较旧的 C 版本要求您在功能块的开头声明所有变量。所以这无论如何都是错误的。

接下来,你的回文逻辑不正确。你只是在开始和结束之间交换字符。

您所要做的就是在迭代 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--;
}

以下是您在工作状态下的完整功能:

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

最新更新