字符串逆序
#include <stdio.h>
#include <string.h>
int main()
{
char *p,*q;
char t;
char str[20] = "gnorts ma I";
p = q = str;
while ('\0' !=*q)
{
q++;
}
q--;//这个while语句实现将指针q指向最后一个有效字符
while (p < q)
{
t = *p;
*p = *q;
*q = t;
p++ ;
q--;
}
fputs (str, stdout);
printf ("\n");
return 0;
}
{
char *p,*q;
char t;
char str[20] = "gnorts ma I";
p = q = str;
while ('\0' !=*q)
{
q++;
}
q--;//这个while语句实现将指针q指向最后一个有效字符
while (p < q)
{
t = *p;
*p = *q;
*q = t;
p++ ;
q--;
}
fputs (str, stdout);
printf ("\n");
return 0;
}
方法二
#include <stdio.h>
#include <string.h>
#include <string.h>
int main()
{
int i,pos;
char t;
char str[20] = {"abcde"};
char *p = str;
pos = strlen(str);
for(i = 0; i < pos/2; i++)
{
t = *(p+i);
*(p+i) = *(p+(pos-1-i));
*(p+(pos-1-i))=t;
}
fputs(str,stdout);
printf("\n");
return 0;
}
{
int i,pos;
char t;
char str[20] = {"abcde"};
char *p = str;
pos = strlen(str);
for(i = 0; i < pos/2; i++)
{
t = *(p+i);
*(p+i) = *(p+(pos-1-i));
*(p+(pos-1-i))=t;
}
fputs(str,stdout);
printf("\n");
return 0;
}