6-3 函數實現字符串逆序(15 分)
本題要求實現一個字符串逆序的簡單函數。
函數接口定義:
void f( char *p );
函數f
對p
指向的字符串進行逆序操作。要求函數f
中不能定義任何數組,不能調用任何字符串處理函數。
裁判測試程序樣例:
#include <stdio.h>
#define MAXS 20
void f( char *p );
void ReadString( char *s ); /* 由裁判實現,略去不表 */
int main()
{
char s[MAXS];
ReadString(s);
f(s);
printf("%s\n", s);
return 0;
}
/* 你的代碼將被嵌在這里 */
輸入樣例:
Hello World!
輸出樣例:
!dlroW olleH
你需要復制的代碼
void f( char *p ) { int i=0,q=0,h,tmp; while(p[i]!='\0') i++; h=i-1; while(q<=h) { tmp=p[q]; p[q]=p[h]; p[h]=tmp; q++; h--; } return ; }