简体   繁体   中英

Copying strings of equal length to new string array

I have a dictionary of words stored in a 2D char array. I also have a scanned word stored in a structure. I'm trying to 'thin down' my main dictionary by copying words of length equal to the scanned word into a seperate 2D array. Then I want to print the new array out.

ie if scanned word = hello , all words of the same length will be copied into the new array.

My code just prints the first word of the array infinitely

words.startword is the scanned word.

void get_equal_length (char equal_length_dictionary[MAX_WORDS][MAX_WORD_LENGTH], char dictionary[MAX_WORDS][MAX_WORD_LENGTH], Scanned_words words)

{
int i, word_count = 0;

for (i = 0; dictionary[i] != '\0'; i++)
    {
        if (strlen(*dictionary) == strlen(words.startword))
            {
                strcpy(*equal_length_dictionary, *dictionary);
                word_count++;
                printf("Word #%d: %s\n", word_count, *equal_length_dictionary);
            }
    }
    printf("Equal length words: %d\n", word_count);
}
 for (i = 0; dictionary[i] != '\\0'; i++) { if (strlen(dictionary) == strlen(words.startword)) { strcpy(*equal_length_dictionary, *dictionary); 

should be:

for (i = 0; dictionary[i][0] != '\0'; i++)
{
    if (strlen(dictionary[i]) == strlen(words.startword))
    {
      strcpy(equal_length_dictionary[i], dictionary[i]);

Also, to improve speed, better calculate strlen(words.startword) only once before the loop, instead of recalculating it inside the loop at each iteration. You should also not forget to terminate the new array with a null string.

The full code will be:

void get_equal_length(char equal_length_dictionary[MAX_WORDS][MAX_WORD_LENGTH], char dictionary[MAX_WORDS][MAX_WORD_LENGTH], Scanned_words words)
{
  int i, word_count = 0, len = strlen(words.startword);

  for (i = 0; dictionary[i][0] != '\0'; i++)
  {
    if (strlen(dictionary[i]) == len)
    {
      strcpy(equal_length_dictionary[i], dictionary[i]);
      word_count++;
      printf("Word #%d: %s\n", word_count, equal_length_dictionary[i]);
    }
  }
  // now we will also terminate the new array with a null string
  equal_length_dictionary[i][0] = '\0';
  printf("Equal length words: %d\n", word_count);
}

This should work, although not tested. As Kevin mentioned in the comment, you need to use the index i in the loop. You also should use word_count as index:

void get_equal_length (char equal_length_dictionary[MAX_WORDS][MAX_WORD_LENGTH], char dictionary[MAX_WORDS][MAX_WORD_LENGTH], Scanned_words words)

{
int i, word_count = 0;

for (i = 0; i < MAX_WORDS; i++)
    {
        char* cur_word = dictionary[i];
        if (!cur_word || !*cur_word)
          break;
        if (strlen(cur_word) == strlen(words.startword))
            {
                strcpy(equal_length_dictionary[word_count], cur_word);
                word_count++;
                printf("Word #%d: %s\n", word_count, equal_length_dictionary[word_count]);
            }
    }
    printf("Equal length words: %d\n", word_count);
}

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