Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

關於C語言一些問題

Complete function trim_blanks whose purpose is to take a single string input parameter (to_trim) and return a copy of the string with leading and trailing blanks removed. Use strncpy in trim_blanks.

就是

a phrase

前面和後面都有空白

要寫trim_blanks函式移除空白

我大致上完成一些程式碼

#include <stdio.h>

#include <string.h>

char *

trim_blanks (char *trimmed, const char *to_trim);

int main()

{

char s_return[20];

char s_out[20];

char s_in[] = " a phrase ";

s_return = trim_blanks(s_out, s_in);

printf("%s\n", s_in);

printf("%s\n", s_out);

printf("%s\n", s_return);

system("pause");

return (0);

}

/*

* Trims leading and trailing blanks from s.

*/

char *

trim_blanks (char *trimmed, /* output */

const char *to_trim) /* input */

{

int subscript_1, subscript_2, i, j, n;

n = strlen(to_trim)

for (i = 0; i < n; i++)

{

if ((int)to_trim[i] != 32)

{

int subscript_1 = i;

i = 20;

}

}

for (j = n - 1; j <= 0; j--)

{

if ((int)to_trim[i] != 32)

{

int subscript_2 = j;

j = -1;

}

}

strncpy(trimmed, &to_trim(subscript_1), subscript_1 - subscript_2 + 1);

return (trimmed);

}

return到底要回傳什麼??

不論怎麼寫都有錯!!

麻煩高手指教!!

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    #include <stdio.h> /* printf, scanf definitions */

    #include <string.h> /* strlen, strncpy definitions */

    /* Function prototype */

    char *

    trim_blanks (char *trimmed, const char *to_trim);

    int main()

    {

    /* Array declarations */

    char *s_return;

    char s_out[20];

    /* Array declarations and get data */

    char s_in[] = " a phrase ";

    /* Call function */

    s_return = trim_blanks(s_out, s_in);

    /* Display result */

    printf("%s\n", s_in);

    printf("%s\n", s_out);

    printf("%s\n", s_return);

    system("pause");

    return (0);

    }

    /*

    * Trims leading and trailing blanks from s.

    */

    char *

    trim_blanks (char *trimmed, /* output */

    const char *to_trim) /* input */

    {

    /* Variable declarations */

    int subscript_1, subscript_2, i, j, n;

    /* Get length of array */

    n = strlen(to_trim);

    /* Search for the first letter */

    for (i = 0; i < n; i++)

    {

    if ((int)to_trim[i] != 32)

    {

    subscript_1 = i;

    i = 20;

    }

    }

    /* Search for the last letter */

    n = n - 1;

    for (j = n; j >= 0; j--)

    {

    if ((int)to_trim[j] != 32)

    {

    subscript_2 = j;

    j = -1;

    }

    }

    /* Use strncpy function to trim blanks */

    strncpy(trimmed, &to_trim[subscript_1], subscript_2 - subscript_1 + 1);

    /* Add '\0' in the end of the array, and it can avoid to show strange sign */

    trimmed[subscript_2 - subscript_1 + 1] = '\0';

    return trimmed;

    }

    Source(s): me
  • 1 decade ago

    我看你的程式..

    你的意思是不是要去除s_in這個字串變數的前後空白..

    中間不動..

    是嗎..

    如果是..你的寫法有點錯誤..

    應該說怪異..

    在trim_blanks這個副程式裡面..

    迴圈..

    for (i = 0; i < n; i++)

    {

    if ((int)to_trim[i] != 32)

    {

    int subscript_1 = i;

    i = 20; <<==這裡出錯..

    }

    }

    i=20..

    我猜想你是想讓他跳離這個回圈吧..

    因為我們已經找到除了空白外的第一個字元..

    因為那行要改成

    break;

    好讓他跳離..

    那行的前面..

    int subscript_1 = i;

    應該也不能有前面的int..

    該參數你已經定義了..

    那個subscript_1參數是紀錄字串的第一個不是空白字元的位置..

    subscript_2應該就是紀錄從尾巴往前推不是空白的字元位置..

    因此兩行

    int subscript_1 = i;

    int subscript_2 = f;

    都不該有前面的int;

    再來後面的迴圈..

    j=-1也是目的要跳離迴圈..

    因此j=-1也是要改成break;

    至於這行..

    strncpy(trimmed, &to_trim(subscript_1), subscript_1 - subscript_2 + 1);

    是要拷貝我們找到的字串..

    功能就是能拷貝幾個字..

    以達到去頭去尾的目的..

    因此寫法要這樣..

    strncpy(trimmed, &to_trim[subscript_1],subscript_2-subscript_1+1);

    不是subscript_1-subscript_2

    因為_2數值比較大..

    所以你寫錯了..

    至於回傳什麼..

    不重要..

    不用改..

    因為你是直接將拷貝的字串直接放進s_out..

    已經把結果回傳到你的主程式參數了..

    參考看看吧..

    2008-11-30 17:03:57 補充:

    改好的副程式..

    char *trim_blanks (char *trimmed,const char *to_trim)

    {

    int subscript_1, subscript_2, i, j, n;

    n=strlen(to_trim)

    for(i=0;i

    2008-11-30 17:04:20 補充:

    for(j=n-1;j<= 0;j--)

    {

    if((int)to_trim[i]!=32)

    {

    subscript_2=j;

    break;

    }

    }

    strncpy(trimmed,&to_trim[subscript_1], subscript_2-subscript_1+1);

    return trimmed;

    }

    2008-11-30 17:05:42 補充:

    有一段迴圈有錯..

    沒貼好..

    for (i = 0; i < n; i++)

    {

    if ((int)to_trim[i] != 32)

    {

    subscript_1 = i;

    break;

    }

    }

Still have questions? Get your answers by asking now.