题2 (a)编写程序,用来读一条消息,然后检查这条消息是否是回文(信息中从左到右的字母和从右到左的字母完全一样):
Enter a message: He lived as a devil, eh?
palindrome
enter a message: Madam, I am Adam.
Not a palindrome
忽略所有不是字母的字符。用整型变量来跟踪数组内的位置。
(b)修改上述程序,使用指针来代替整数跟踪数组的位置。
答:(a)程序如下:
#include<stdio.h>
#include<stdlib.h>
#define N 100
#define true 1
#define false 0
int main()
{
typedef int Bool;
Bool palindrome;
char ch[N];
int i=0,m;
printf(“Enter a message:”);
do{
ch[i]=getchar();
if(i==N-1)
{
printf("The array is full\n");
break;
}
}while(ch[i++]!='\n');
i=i-2;
for(m=0;m<=i;)
{
if((ch[m]>='a'&&ch[m]<='z')||(ch[m]>='A'&&ch[m]<='Z'