编写一个程序来反转存储在以下指向字符串的指针数组中的字符串:
char *s[ ] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
提示:编写一个函数
xstrrev ( string )
,它应该反转一个字符串的内容。调用此函数以反转存储在s
.
为什么我无法使用此代码获得正确的输出?
#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "string.h"
using namespace System;
void xstrrev (char str[])
{
int i,l;
char temp;
l=strlen(str);
for (i=0;i<(l/2);++i)
{
temp=*(str+i);
*(str+i)=*(str+i+l-1);
*(str+i+l-1)=temp;
}
}
void main ()
{
char *s[] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
xstrrev(s[0]);
xstrrev(s[1]);
xstrrev(s[2]);
puts(s[0]);
puts(s[1]);
puts(s[2]);
getch();
}