C 如何动态创建字符串数组,同时从数组项中删除空格?

C 如何动态创建字符串数组,同时从数组项中删除空格?,c,arrays,string,malloc,C,Arrays,String,Malloc,基本上,我希望我的这部分代码从输入的第一行读取句子的数量,然后读取句子本身,并将它们存储在一个数组中(即使输入可以包含空格,但最后的数组条目可能不包含,而不是大写)。 对于以下输入 3 one two three four five six (program didn't let me input another line, but just two more for the sake of the example.) stack over flow 我想要以下输出 onetwothree f

基本上,我希望我的这部分代码从输入的第一行读取句子的数量,然后读取句子本身,并将它们存储在一个数组中(即使输入可以包含空格,但最后的数组条目可能不包含,而不是大写)。 对于以下输入

3
one two three
four five six (program didn't let me input another line, but just two more for the sake of the example.)
stack over flow
我想要以下输出

onetwothree
fourfivesix
stackoverflow
斩首还没有实施,但我想这并不难。 使用我的代码:

void main(){
int length1, length2,i,n;

scanf("%d", &length1);
char *sentenceArray1[length1];
char tempString[100000];
/*The array for storing the first set of sentences, and a temporary string used
for allocating memory in the next loop*/
for(i=0;i<=length1;i++){
        fgets(tempString, 100000, stdin);
        sentenceArray1[i]=malloc((strlen(tempString))*sizeof(char));
        sentenceArray1[i]=tempString;
        for(n=0;n<(strlen(tempString));n++){
                if(tempString[n]==' '){
                        sentenceArray1[i][n]=tempString[n+1];
                        n++;
                }
        printf("%s",sentenceArray1[i]);
        }

}

如果标记太离谱,我很抱歉,这是我第一次发布问题。

考虑以下几点:

one two three
one two three
one two three
onettwo three
onettwo three
onettwo three
onettwotthree
onettwotthree
onettwotthree
onettwotthree
onettwotthree
onettwotthree
代码

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

#define MAX_STR_LEN (100000)

int main(void)
{
    int numStrings, tempIndex, modIndex, numSpaces;
    char tempString[MAX_STR_LEN];

    printf("Enter number of strings: ");
    scanf("%d", &numStrings);

    while(getchar() != '\n')
        continue;

    char **modifiedStrings = malloc(numStrings * sizeof(*modifiedStrings));

    for(int i = 0; i < numStrings; i++)
    {
        printf("Enter string %d: ", i + 1);
        fgets(tempString, MAX_STR_LEN, stdin);

        tempIndex = numSpaces = 0;
        while(tempString[tempIndex] != '\n')
        {
            if(tempString[tempIndex++] == ' ')
                numSpaces++;
        }

        modifiedStrings[i] = malloc(strlen(tempString) - numSpaces + 1);

        tempIndex = modIndex = 0;
        while(tempString[tempIndex] != '\n')
        {
            if(tempString[tempIndex] != ' ')
                modifiedStrings[i][modIndex++] = tempString[tempIndex];

            tempIndex++;
        }
        modifiedStrings[i][modIndex] = '\0';
    }

    for(int i = 0; i < numStrings; i++)
        printf("%s\n", modifiedStrings[i]);

    return 0;
}
#包括
#包括
#包括
#定义最大长度(100000)
内部主(空)
{
int numstring、tempIndex、modIndex、numSpaces;
字符临时字符串[MAX_STR_LEN];
printf(“输入字符串数:”);
scanf(“%d”和numStrings);
而(getchar()!='\n')
继续;
char**modifiedStrings=malloc(numStrings*sizeof(*modifiedStrings));
对于(int i=0;i
逻辑

  • 了解将输入多少字符串。将它们存储在变量(
    numStrings
    )中
  • 删除
    scanf
    留下的换行符(
    '\n'
  • 创建一个
    char*
    数组,为每个要输入的字符串创建一个元素
  • 获取每个字符串。将其存储到临时的
    char
    数组(
    tempString
    )中
  • 计算临时
    char
    字符串中的空格数(
    '
  • malloc
    只有足够的内存满足您的需要
  • 将临时字符串中的每个字符复制到新字符串(
    modifiedStrings[i]
    ),跳过空格
  • NULL
    '\0'
    )附加到新的
    char
    数组的末尾,使其成为字符串
  • 向某人击掌
  • 要做的事

  • 错误检查
  • 示例运行

    输入字符串数:3
    输入字符串1:1-2-3
    输入字符串2:四五六
    输入字符串3:堆栈溢出
    一对三
    fourfivesix
    堆栈溢出


    在一行中使用malloc分配内存,然后在下一行中丢失对它的引用:
    sentenceArray1[i]=tempString。最后所有的sentenceArray[i]都指向tempString。您可能希望改为
    strcpy()
    strncpy()
    tempString。