编写一个程序,反序显示命令行参数的单词。例如,命令行参数是see you later ,该程序应打印 later you see 。
此类题的关键点是,临时指针数组保存每个单词的首地址,然后对首地址进行交换操作。然后从首地址打印整个单词。
实现的代码如下:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
int j;
i = 0;
j = argc - 1;
char * temp[argc];
char *s;
for(i=0;i<argc;i++)
temp[i] = argv[i+1];
i = 0;
while(i<j){
s = temp[i];
temp[i] = temp[j];
temp[j] = s;
i++;
j--;
}
for(i = 1; i<argc;i++)
printf("%s " ,*(temp + i));
puts("");
return 0;
}