分类: C/C++
2011-06-09 20:20:15
方法一:
void Reverse(char *src)
{
char *des = NULL;
int len = strlen(src);
des = (char *)malloc((len+1)*sizeof(char));
if (des == NULL)
{
exit(-1);
}
char *d = des;
char *s = &src[len-1];
while (len-- != 0)
{
*d++ = *s--;
}
*d = '\0';
cout<
cout<
free(des);
}
方法二:在字符串自身上操作,不引入其他的变量
void Reverse(char *src)
{
int len = strlen(src);
char tmp;
for (int i = 0; i < len/2; i++)
{
tmp = src[i];
src[i] = src[len - 1 - i];
src[len - 1 - i] = tmp;
}
cout<
}
方法三:递归方法
void Reverse(char *src)
{
if (*src == '\0')
return;
Reverse(src+1);
printf("%c", *src);
}