0031 字符串逆序

/*6-12 函數實現字符串逆序 (15 分)
本題要求實現一個字符串逆序的簡單函數。
函數接口定義:
void f( char *p );
函數f對p指向的字符串進行逆序操作。要求函數f中不能定義任何數組,不能調用任何字符串處理函數。
裁判測試程序樣例:*/
#include <stdio.h>
#define MAXS 20

void f( char *p );

int main()
{
    char s[MAXS];

    gets(s);
    f(s);
    printf("%s\n", s);

    return 0;
}
#include<string.h>
void f( char *p ){
    int i,j,n,t;
    j=strlen(p);
    n=j;
    for(i=0;i<=j;i++,j--){
        t=p[i];
        p[i]=p[j-1];
        p[j-1]= t;
    }
}
注:1.注意該循環的條件,i<=j;如果是i<=n,會先逆序再逆序,結果不變

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章