how can i store an int array into string [closed]

Deadly 提交于 2021-02-18 12:10:44

问题


I have an integer array:

int a[5]={5,21,456,1,3}

I need to store these number into char array so that the char array will have some thing like this:

char *s="52145613";

Is there any library function in c for this?


回答1:


sprintf do what you need.

Little example

char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += sprintf(&str[index], "%d", a[i]);

snprintf takes care of the str length

char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += snprintf(&str[index], 128-index, "%d", a[i]);



回答2:


What you need to do is to convert the integer values in a into character values in s leaving room for a null-terminating character. In your case a total of 9 characters. sprintf is the proper tool since vales exceed single digits:

#include <stdio.h>

int main(void)
{
    int a[5]={5,21,456,1,3};
    char s[9] = {0};
    int n = 0;

    for (int i = 0; i < 5; i++) {
        n += sprintf (&s[n], "%d", a[i]);
    }

    printf ("\n char* s = %s\n\n", s);

    return 0;
}

Output

$ ./bin/sprintf

 char* s = 52145613



回答3:


Tried to go for a more readable approach even though the other answers are more efficient in terms of memory and instructions.

Tested it here.

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

void get_me_a_string(int * int_array, int array_size, char * output_string, int output_string_max_size)
{
    if(!int_array || !output_string)
        return;

    char * aux_string = NULL;

    //Depending on the compiler int is 2-byte or 4 byte.
    //Meaning INT_MAX will be at most 2147483647 (10 characters + 1 '\0').
    aux_string = (char *) malloc(11);
    if(!aux_string)
        return;

    int i;
    int current_array_size = 0;
    for(i = 0; i < array_size; i++)
    {
        sprintf(aux_string, "%d", int_array[i]);
        current_array_size += strlen(aux_string);
        if(current_array_size < output_string_max_size)
            strcat(output_string, aux_string);
        else
            break;
    }

    free(aux_string);
}

int main(void) {
    int a[5]={5,21,456,1,3};

    int string_max_size = 256;
    char * string_from_array = NULL;

    string_from_array  = (char *) malloc(string_max_size);

    if(NULL == string_from_array)
    {
        printf("Memory allocation failed. Exiting...");
        return 1;
    }

    memset(string_from_array, 0, string_max_size);
    get_me_a_string(a, 5, string_from_array, string_max_size);

    printf(string_from_array);

    free(string_from_array);
    return 0;
}



回答4:


You can search for sprintf()/ snprintf() which can get the job done for you.

From the man page,

int sprintf(char *str, const char *format, ...); int snprintf(char *str, size_t size, const char *format, ...);

The functions in the printf() family produce output according to a format as described below....[...]...

sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.




回答5:


Here is a demonstrative program that shows how this can be done.

#include <stdio.h>

int main(void) 
{
    int a[] = { 5, 21, 456, 1, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );
    char s[10];
    int n;

    for ( size_t i = 0, j = 0; 
          i < N && ( n = snprintf( s + j, sizeof( s ) - j, "%d", a[i] ) ) > 0;
          i++ )
    {
        j += n;
    }        

    puts( s );

    return 0;
}

The program output is

52145613


来源:https://stackoverflow.com/questions/30234363/how-can-i-store-an-int-array-into-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!