简体   繁体   中英

how to create a string from an array of strings C

I am learning about arrays and was wondering if someone can help me out. I have an array of strings and need to create a new string which is a concatenation of all the array elements. The problem I'm having is I'm only able to print the first string in my array, not all of them. I understand there is a null at the end of each string in my array so how would I work around that issue? Maybe 2d array? By the way I'm not allowed to use any string manipulation functions from string.h. Thank you.

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


int findLength(char array[])
{
    int i = 0;
    for (i = 0; array[i] != '\0'; i++)
    {
    }
    return i;
}

void arrayToString(char string[])
{
    int n = 0;
    int i = 0;
    int l = findLength(string);
    char *finalString;
    finalString = malloc(l * sizeof(char));
    for (i = 0; string[i] != '\0'; i++) {
        finalString[n] = string[i];
        n++;
    }
    for (i = 0; finalString[i] != '\0'; i++) {
        printf("%c", finalString[i]);
    }
}

int main(int argc, const char * argv[])
{
    char *color[] = { "red", "blue", "red" };
    arrayToString(*color);
    return 0;
}

you have several problems in your code, here is the fixed version with comments:

size_t findLength(char* array[]) {
    size_t l = 0;
    while (char *t = *array++)
        while (*t++)
            l++;
    return l;
}

void copyAll(char* array[], char* out) {
    while (char *t = *array++)
        while (*t)
            *out++ = *t++; // copy every symbol from every line into out string
    *out = '\0'; // append last null-terminator
}

void arrayToString(char* array[]) {
    char* finalString = malloc((findLength(array) + 1) * sizeof(char)); // allocate + 1 symbol for null terminator
    copyAll(array, finalString);
    printf("%s", finalString);
    free(finalString); // don't forget to release memory
}

int main(int argc, char* argv[]) {
    char* color[] = { "red", "blue", "red", 0 }; // you should add array terminator as well
    arrayToString(color);
    return 0;
}

Change your function arrayToString to have two arguments.one of type char ** and the second of type size_t defining the number of strings.Also let its return value to be char * to return a pointer to the allocated memory.finally don't forget to free this memory.

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


int findLength(char array[]) {
    int i = 0;
    for (i = 0; array[i] != '\0'; i++) {
    }
    return i;
}

char* arrayToString(char **string, size_t size) {
    int bigSize = 0, len;
    int i = 0, j, k;
    for (j = 0; j < size; j++) {
        bigSize += findLength(string[j]);
    }
    char *bigstring = (char *)malloc(bigSize + 1);
    for (j = 0; j < size; j++) {
        len = findLength(string[j]);
        for (k = 0; k < len; k++) {
            bigstring[i++] = string[j][k];
        }
    }
    bigstring[i] = '\0';
    return bigstring;
}

int main(int argc, const char * argv[])
{
    char *color[] = { "red", "blue", "red" };
    char *bigstring = arrayToString(color, 3);
    printf("%s\n", bigstring);
    free(bigstring);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM