简体   繁体   English

如何从字符串数组C创建一个字符串

[英]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? 我知道数组中每个字符串的末尾都有一个null,所以我将如何解决该问题? Maybe 2d array? 也许二维数组? By the way I'm not allowed to use any string manipulation functions from string.h. 顺便说一句,我不允许使用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. 改变你的函数arrayToString有两个参数,一个是char **类型,另一个是size_t类型,用于定义字符串的数量,也让它的返回值为char *以返回一个指向分配的内存的指针,最后不要忘记释放此内存。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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