您的位置:首页 > 其它

翻转字符串

2015-11-26 21:15 316 查看
例如,输入"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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: