例如,输入"I am a student.",经过字符串翻转之后,输出"student. a am I"。
思路:先将"I am a student."放入到字符串数组str[]中,然后将所有字符进行逆序翻转,得到".tneduts a ma I",然后针对每个单词(以空格为划分标准)进行翻转,可得结果。
#include<stdio.h>
int main()
{
char str[]="I am a student.";
char *p,*q;p=q=str;
printf("%s\n",str);
while(*q!='\0') q++;
q--;
while(p<=q)
{
char temp=*p;*p=*q;*q=temp;
p++;q--;
}
printf("%s\n",str);
char *next;p=q=next=str;
while(*next!='\0')
{
if(*next==' ')
{
q--;
while(p<=q)
{
char temp=*p;*p=*q;*q=temp;
p++;q--;
}
//p=++next;q=next;
p=q=++next;
}
next++;q++;
}
printf("%s\n",str);
return 0;
}
//I am a student.翻转字符串之后变成student. a am I