문제

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

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
    }
}

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

I have a string:

'     ABCDEF/G01        '

The above function is supposed to remove the spaces and return to me:

'ABCDEF/G01'.

Instead, what I get back is:

'ABCDEF/'

Any ideas?

Note: the quotes are just to show you that spaces exist in the original string.

도움이 되었습니까?

해결책

The strncpy is wrong. sizeof(dest) is not what you want (it's the size of a pointer on your machine). You probably want: end - front. Instead, try:

memcpy(dest, front + start, end - front);
dest[end] = 0;

다른 팁

The sizeof(dest) doesn't do what you think it does! It returns the size of the pointer, not the length of the string. You need to supply a maximum length of the destination to your function.

For the string orig you want to use the strlen function.

size_t end = sizeof(orig) - 1;
strncpy(dest, tmp, sizeof(dest) - 1);

You probably want strlen instead of sizeof here.

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

In that code, sizeof(orig) is the size of a pointer. All pointers are the same size, probably 8 in your implementation. What you want to use is strlen(orig) instead.

Try this code (it doesn't use temporary memory):

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++;
        }
    }
}

NOTE: Not tested!

You must replace sizeof() to strlen() everywhere in your function. Here is working edit:

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
    }
}

(I've tested it)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top