C語言問題!!請高手協助一下!!

輸入一字串,存到陣列,再將其字串做反轉,存回到原陣列,並將反轉後的字串印出

字串反轉請寫成一個函式

Ex: convert(char data[]);

Update:

輸入一字串,存到陣列,再將其字串做反轉,存回到原陣列,並將反轉後的字串印出

字串反轉請寫成一個函式

Ex: convert(char data[]); a,b,c,d

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    void reverse(char d[]) {

    char *loPtr = d; // low side pointer

    char *hiPtr = d; // high side pointer

    char c;

    while('\0' != *hiPtr) ++hiPtr; // scan to the end of the string

    --hiPtr; // point to the last char in string

    while(loPtr < hiPtr) c=*loPtr, *loPtr++ = *hiPtr, *hiPtr-- = c;

    return;

    } // end of void reverse()

    2009-12-17 13:19:03 補充:

    add this before the return:

    printf("%s", d); // print the reversed string

    2009-12-17 21:55:09 補充:

    I really hate what I wrote, and will rewrite the function body below:

    for(hiPtr=d;'\0' != *hiPtr; ++hiPtr); // scan to the end of the string

    for(--hiPtr, loPtr=d;loPtr < hiPtr; ++loPtr, --hiPtr) c=*loPtr, *loPtr=*hiPtr, *hiPtr=c;

    printf("%d",d);

  • 1 decade ago

    一定要特別拉出來做成一個函式嗎

Still have questions? Get your answers by asking now.