<span style="font-size:24px;">//将student a am i 转换成 i am a student
#include<stdio.h>
#include<string.h>
//翻转一个单词
/*void reverse_string(char *l,char*r)
{
while(l<r)
{
char tmp;
tmp=*l;
*l=*r;
*r=tmp;
l++;
r--;
}
}
//由空格判断一个单词,调用reverse_string函数进行翻转
void reverse(char ch[],int len)
{
int i=0;
char *ctmpl=ch;
char *ctmpr=ch;
while(ch[i])
{
if(ch[i]==' ')
{
reverse_string(ctmpl,ctmpr-1);
ctmpl=++ctmpr;
}
else
ctmpr++;
i++;
}
reverse_string(ch,ch+len-1);//翻转整个字符串
}
int main()
{
char ch[]="student a am i";
reverse(ch,strlen(ch));
printf("%s\n",ch);
return 0;
}</span>
字符串翻转
最新推荐文章于 2024-02-02 07:53:35 发布