/*昨天看到说是面试题,看了看是字符翻转问题*/
#include
/*整句翻转*/
char *strrev( char *src)
{
char *begin, *end, temp = '\0';
begin = end = src;
if( src == NULL)
return NULL;
while( *end != 0)
{
end++;
}
end -= 1;
for( ; begin < end; begin++, end--)
{
temp = *begin;
*begin = *end;
*end = temp;
}
return src;
}
/*局部翻转*/
char *revpart( char *src)
{
char *begin, *end, *dumyend, temp = '\0';
begin = end = dumyend = src;
if( src == NULL)
return NULL;
while( *dumyend != '\0')
{
while( *end != '\0' && *end != ' ')
{
end++;
}
dumyend = end;
end -= 1;
for( ; begin < end; begin++, end--)
{
temp = *end;
*end = *begin;
*begin = temp;
}
while( *dumyend == ' ')
dumyend++;
begin = end = dumyend;
}
return src;
}
/*main*/
int main(void)
{
char a[] = "I am a student";
char *ret;
strrev(a);
printf("%s\n", a);
revpart(a);
printf("%s\n", a);
return 0;
}
/*
*总结:字符串翻转,双次翻转位置不变,单次翻转,相对位置对称翻转。
*/
阅读(1305) | 评论(0) | 转发(0) |