簡體   English   中英

C中沒有庫的字符串連接

[英]String concatenation without libraries in 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", 2}, {"o", 1}]result應該是hello 但是,此代碼打印h l o ,其中第二個和第四個字母是問號。 誰能幫我調試一下?

保持簡單,錯誤會自行修復。 不要將result緩沖區中的位置與循環迭代器混淆。 不需要臨時變量。

#include <stdio.h>

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

void concat(word words[], int arraySize, int maxSize) { 
  char result[maxSize];
  int count=0;
  
  for(int i=0; i<arraySize; i++)
  {
    for(int j=0; j<words[i].wordSize; j++)
    {
      result[count]= words[i].str[j];
      count++;
    }
  }
  result[count] = '\0';
  
  puts(result);
}

int main()
{
  word w[3] = { {"he", 2}, {"ll", 2}, {"o", 1} };
  concat(w, 3, 128);
}

在這個 for 循環中

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

您同時增加resultSizej ,而您只需要增加變量j並且在循環之后將變量resultSize增加j

但無論如何該函數都是錯誤的,因為沒有檢查resultSize是否小於maxSize

此外,構建的字符串沒有附加終止零'\\0' 結果這個說法

puts(result);

調用未定義的行為。

無需創建變長數組

char result[maxSize];

只是為了輸出連接的字符串。

該函數可以通過以下方式聲明和定義,如下面的演示程序所示。

#include <stdio.h>

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

void concat( const struct word words[], size_t arraySize, size_t maxSize )
{
    for ( size_t i = 0, j = 0; i < maxSize && j < arraySize; j++ )
    {
        for ( size_t k = 0; i < maxSize && k < words[j].wordSize; i++, k++ )
        {
            putchar( words[j].str[k] );
        }
    }
    
    putchar( '\n' );
}

int main(void) 
{
    struct word words[] =
    {
        { "he", 2 }, { "ll", 2 }, { "o", 1 }
    };
    const size_t arraySize = sizeof( words ) / sizeof( *words );
    
    concat( words, arraySize, 5 );
    
    return 0;
}

程序輸出是

hello

你在這里有幾個問題。 result[resultSize + j]意味着你實際上跳過了結果中的一半以上的字符。 其次,如果您復制整個字符串,包括空字符, puts將停止在第一個單詞本身的末尾打印結果字符串。 因此,您需要跳過復制字符串終止符並將其放在整個連接字符串的末尾。

#include <stdio.h>

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

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

  for (int i = 0; i < 3; i++) {
    tmp = words[i];
    // Keep copying the words to result, one after the other
    // But drop the last NULL character
    for (int j = 0; j < (words[i].wordSize - 1); j++,  resultSize++) {      
      result[resultSize] = tmp.str[j]; 
    }
  }
  // Put the NULL character in after all words have been copied
  result[resultSize] = 0;
  puts(result);
}

struct word mywords[] = 
{
  { "TestWord", sizeof("TestWord")},
  { "TestWord2", sizeof("TestWord2")}
};

int main(void)
{
  concat(mywords, 100);
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM