次の文字列へのポインターの配列に格納されている文字列を逆にするプログラムを作成します。
char *s[ ] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
ヒント:
xstrrev ( string )
1 つの文字列の内容を逆にする関数を書きます。に格納されている各文字列を反転するには、この関数を呼び出します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();
}