有一个字符数组的内容为:"student a am i",请你将数组的内容改为"i am a student".
要求:
不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)。
#include<stdio.h>
#include<assert.h>
int my_len(char *str)
{
int count=0;
assert(str);
while(*str)
{
count++;
str++;
}
return count;
}
void reverse(char *start,char *end)
{
while(start<end)
{
char tmp=*start;
*start=*end;
*end=tmp;
start++;
end--;
}
}
void rev(char *s)
{
int len=my_len(s);
char *start=s;
char *end=s+len-1;
reverse(start,end);
while(*s)
{
start=s;
while((*s!=‘ ‘)&&(*s!=‘\0‘))
{
s++;
}
end=s-1;
reverse(start,end);
if(*s == ‘ ‘)
s++;
}
}
int main()
{
char s[]="student a am i";
rev(s);
printf("%s\n",s);
system("pause");
return 0;
}将数组"student a am i"的内容改为"i am a student"
原文:http://760470897.blog.51cto.com/10696844/1708337