没有C语言库的字符串连接

问题描述

我在没有库函数的情况下在C中连接字符串时遇到麻烦。 我尝试了以下方法:

#include <stdio.h>

struct word {
  char *str;
  int wordSize;
};

void concat(struct word words[],int arraySize,int maxSize) { // word array,its size,and max size given
  char result[maxSize];
  int resultSize = 0;
  struct word tmp;

  for (int i = 0; i < arraySize; i++) {
    tmp = words[i];
    for (int j = 0; j < words[i].wordSize; j++,resultSize++) {
      result[resultSize + j] = tmp.str[j];
    }
  }

  puts(result);
}

例如,如果结构数组words包含[{"he",2},{"ll",{"o",1}],则result应该为hello。但是,此代码会打印h�l�o,其中第二个和第四个字母是问号。谁能帮我调试一下吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)