3

================================================== =============================

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;
    size_t counter = 0;
    char * tmp = null;

    if (sizeof(orig) > 0)
    {
        memset(dest, '\0', sizeof(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
                front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
                end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, sizeof(dest) - 1);
        free(tmp); //strndup automatically malloc space
    }
}

================================================== =============================

私は文字列を持っています:

'ABCDEF / G01'

上記の関数は、スペースを削除して私に戻ることになっています。

'ABCDEF/G01'

代わりに、私が返すのは次のとおりです。

「ABCDEF/」

何か案は?

注:引用符は、元の文字列にスペースが存在することを示すためのものです。

4

6 に答える 6

4

strncpy間違っています。sizeof(dest)あなたが望むものではありません(それはあなたのマシン上のポインタのサイズです)。あなたはおそらく欲しいでしょう:end - front。代わりに、次を試してください。

memcpy(dest, front + start, end - front);
dest[end] = 0;
于 2011-11-11T15:08:36.610 に答える
2

あなたsizeof(dest)が思っていることをしません!文字列の長さではなく、ポインタのサイズを返します。関数に宛先の最大長を指定する必要があります。

関数origを使用する文字列に対して。strlen

于 2011-11-11T15:10:37.653 に答える
1
size_t end = sizeof(orig) - 1;
strncpy(dest, tmp, sizeof(dest) - 1);

ここでは、sizeofの代わりにstrlenが必要になる可能性があります。

于 2011-11-11T15:08:38.447 に答える
1
void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;

そのコードでsizeof(orig)は、はポインタのサイズです。すべてのポインターは同じサイズであり、実装ではおそらく8です。strlen(orig)代わりに使用したいものです。

于 2011-11-11T15:08:48.290 に答える
0

関数内のすべての場所でsizeof()をstrlen()に置き換える必要があります。これが編集作業です:

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;
    char * tmp = NULL;

    if (strlen(orig) > 0)
    {
        memset(dest, '\0', strlen(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
            front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
            end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, strlen(dest));
        free(tmp); //strndup automatically malloc space
    }
}

(私はそれをテストしました)

于 2011-11-11T15:17:34.660 に答える
0

このコードを試してください(一時メモリを使用しません):

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;

    *dest = '\0';

    if (strlen(orig) > 0)
    {    
        /* Find the first non-space character */
        while (front < end && isspace(orig[front]) )
        {
                front++;
        }
        /* Find the last non-space character */
        while (front < end && isspace(orig[end]))
        {
                end--;
        }

        counter = front;
        while ( counter <= end )
        {
                dest[counter-front] = orig[counter];
                counter++;
        }
    }
}

注:テストされていません!

于 2011-11-11T15:17:23.237 に答える