逆序排列

1.逆序排列
#include
#include
#includeint main()
{
char *p = (char * )malloc(sizeof(char) * 100);
printf("please input ; \n");
scanf("%s", p); int length,i;
length = strlen(p);
char *q = p + length - 1;
for(i = 0; i < length; i++)
{
printf("%c", *q);
q--;
}
printf("\n");
free(p);
renturn 0;
} 二、利用指针函数 #include
#include
#includechar *reverse(char *s)
{
char *q = (char*)malloc(sizeof(char)* 20);
if (NULL == q)
{
printf("malloc failure\n");
}
int length = strlen(s);

q = q + length -1;
int i; for(i = 0; i < length; i++)
{
*q-- = *s++;
}
q++; return q;
}
int main()
{
char *tmp;
char *p = (char*)malloc(sizeof(char)* 20);
if (NULL == p)
{
printf("malloc failure\n");
} printf("please input \n");
scanf("%s", p); tmp = reverse(p);

printf("%s\n",tmp);
free(p);
free(tmp);
return 0;
}

    推荐阅读