4. (a) 编写程序,用来读一条消息,然后反向显示出这条消息。程序的输出格式如下:
Enter a message: Don't get mad, get even.
Reversal is: .neve teg ,dam teg t ' noD
提示:读消息一次读取一个字符(用getchar函数),并且把这些字符存储在数组中,当数组满了或
者读到字符'\n'时停止读操作。
(b)修改上述程序,用指针来代替整数来跟踪数组中的当前位置。
(a) 程序如下:
#include <stdio.h>
#define N 100
main(){
char a[N];
int i, num = 0;
printf("Enter a message:");
for(i = 0; i < N; i++){
a[i] = getchar();
if(a[i] == '\n') break;
num++;
}
printf("Reversal is: ");
for(i = num - 1; i >= 0; i--){
putchar(a[i]);
}
return 0;
}
运行结果如下: