2

So I have something like this:

int array[5] = {1, 6, 2, 4, 4};
char string[255];

/*do something*/

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

Output should be:

[1, 6, 2, 4, 4]

I really don't know where to start...

4
  • Which step exactly is unclear to you? You can use sprintf to write numbers to a char-buffer and adding the other characters is not that complicated either Commented Jun 13, 2020 at 15:00
  • Why [1, 6, 2, 5, 5]? If this is the array {1, 6, 2, 4, 4};. Commented Jun 13, 2020 at 15:01
  • @Lucas the output is an char string so string[0] = "[" string[1] = "1" string[2] = "," .... string[14] = "]" Ah, sorry. I wrote it bad Commented Jun 13, 2020 at 15:03
  • Well, you say you don't know where to start. The first step would apparently be to assign '[' to str[0]. Thereafter you need to at least try to write an algorithm of your own. But I can't see anything of that in the question. Commented Jun 13, 2020 at 15:18

3 Answers 3

3

the following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. demonstrates the use of strcpy() and sprintf()
  4. avoids the use of 'magic' numbers
  5. lets the compiler calculate the number of entries in the array[]

and now, the proposed code:

#include <stdio.h>
#include <string.h>


int main( void )
{
    int array[] = {1, 6, 2, 4, 4};
    char string[255] = {0};

    /*do something*/
    strcpy( string, "[" );
    for( size_t i = 0; i < (sizeof(array)/sizeof(int)) -1; i++ )
    {
        sprintf( &string[ strlen(string) ],  "%d, ", array[i] );
    }

    sprintf( &string[ strlen(string) ],  "%d", array[4] );
    strcat( string, "]" );

    printf("%s\n", string);
}

a run of the proposed code results in:

[1, 6, 2, 4, 4]
Sign up to request clarification or add additional context in comments.

1 Comment

only comment is that it has a fixed size output buffer but doesnt defend against overruns, clearly works here but since you say you allow for arbitrary size array ...
-1

You need to convert the integer into equivalent char using the ascii table subtraction and then strcat to the main string

Comments

-1

To convert the array to a string, you should use snprintf:

#include <stdio.h>

char *join(char *dest, size_t size, const int *array, size_t count) {
    if (size == 0) {
        return NULL;
    }
    if (size == 1) {
       dest[0] = '\0';
       return dest;
    }
    size_t pos = 0;
    dest[pos++] = '[';
    dest[pos] = '\0';
    for (size_t i = 0; pos < size && i < count; i++) { 
        int len = snprintf(dest + pos, size - pos, "%d%s",
                           array[i], (i + 1 < count) ? ", " : "]");
        if (len < 0)
            return NULL;
        pos += len;
    }
    return dest;
}

int main() {
    int array[5] = { 1, 6, 2, 4, 4 };
    char string[255];

    if (join(string, sizeof string, array, sizeof(array) / sizeof(*array))) {
        printf("%s\n", string);
    }
    return 0;
}

2 Comments

How about making join return NULL if the snprintf returns negative number, and then in main, test the return and don't call printf? Currently it looks like it might end up calling printf on a string that isn't \0-terminated if that happened, so probably segfault.
@alaniwi: good point. snprintf is highly unlikely to return -1, except on legacy systems where it could indicate that the output exceeds the available space, which is non-standard conforming. Answer updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.