【题目5-6】编程判断输入的一串字符串是否为“回文”,所谓“回文”是指顺读和倒读都一样的字符串,入?

2024-11-16 18:55:02
推荐回答(1个)
回答(1):

#include
#include
#include

/*
* arr[] 字符数组
* halNum 字符数组的一半
* Totmun 字符数组的总长度
*/
bool charf(char arr[], int halNum, int TotNum)
{
if (!arr)
{
return false;
}

bool b = false;
if (arr[halNum] == arr[TotNum - halNum] && halNum == 0)
{
b = true;
}
else if (arr[halNum] == arr[TotNum - halNum])
{
b = charf(arr, halNum - 1, TotNum);
}

return b;

}

int main(int argc, char *argv[])
{
int count = 0;
bool b = false;
char a;
char *arr = (char *)malloc(15);
char *temp1 = arr;
char *temp2 = arr;
memset(arr, '/0', 15);
while ((a = getchar()) != '/n')
{
arr[count++] = a;
}
//除去空格
count = 0;
while (*temp1 != '/0')
{
if (*temp1 != ' ')
{
*temp2++ = *temp1;
count++;
}
temp1++;
}

if (0 == count % 2)
{
b = charf(arr, (count / 2), count - 1);
}
else
{
b = charf(arr, (count / 2 - 1), count - 1);
}

if (b)
{
printf("%s/n", "回文");
}
else
{
printf("%s/n", "不是回文");
}

return 0;
}