字符串数组,strcpy和字符串的问题

时间:2015-11-15 17:33:31

标签: c arrays string strcpy

我在使用字符串和字符串数组以及正确使用strcpy时遇到了麻烦。我正在使用二维数组dictionary中扫描的单词字典。然后我开始一个单词,改变它的每个字母以创建许多不同的变体,即cat -> cbt, cct, cdt等。从那里我将每个生成的单词复制到2D数组中,并将这些生成的单词与字典进行比较,看看是否他们是真实的话。然后,我想要打印这些真实的单词,即cat,如果它在字典中,则生成bat,但zat将不会生成check_dictionary。当我运行代码时,它打印所有生成的单词,但当它到达mat yes cat hat 函数时,它不打印任何单词。

它从中读取的文本文件如下:

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

#define MAX_WORDS 20000
#define MAX_WORD_LENGTH 30
#define ARGS_REQUIRED 2

typedef struct scanned_words
{
  char startword[MAX_WORD_LENGTH];
  char endword[MAX_WORD_LENGTH];
} Scanned_words;

Scanned_words scan_two_words(Scanned_words words);
void get_next_word(Scanned_words words,
    char parentwords[MAX_WORDS][MAX_WORD_LENGTH]);
void read_file(char * argv[], char dictionary[MAX_WORDS][MAX_WORD_LENGTH]);
void check_dictionary(char dictionary[MAX_WORDS][MAX_WORD_LENGTH],
    char parentwords[MAX_WORDS][MAX_WORD_LENGTH]);
void usage(char * argv[]);

int main(int argc, char * argv[])
{
  char dictionary[MAX_WORDS][MAX_WORD_LENGTH];
  char nextword[MAX_WORDS][MAX_WORD_LENGTH];
  char parentwords[MAX_WORDS][MAX_WORD_LENGTH];
  Scanned_words words;

  if (argc == ARGS_REQUIRED)
  {
    system("clear");
    read_file(&argv[1], dictionary);
    words = scan_two_words(words);
    get_next_word(words, parentwords);
    check_dictionary(dictionary, parentwords);
  }
  else
  {
    usage(&argv[0]);
  }

  return 0;
}

void read_file(char * argv[], char dictionary[MAX_WORDS][MAX_WORD_LENGTH])
//reads the text file and stores the dictonary as a 2D array
{
  FILE * file_name;
  int word_count = 0, i;

  if ((file_name = fopen(argv[0], "r")) == NULL )
  {
    printf("Cannot open file ... \n");
  }
  while (fscanf(file_name, "%s", dictionary[i++]) == 1)
  {
    printf("%s ", dictionary[word_count]);
    word_count++;
  }

  printf("\n");
  printf("\n%d words scanned in from: %s\n\n", word_count, argv[0]);
}

Scanned_words scan_two_words(Scanned_words words)
//takes an empty structure, scans both words in and returns them in the same structure
{
  printf("Enter the start word: \n");
  scanf("%s", words.startword);
  printf("\nEnter the end word: \n");
  scanf("%s", words.endword);
  printf("\n");

  return words;
}

void get_next_word(Scanned_words words,
    char parentwords[MAX_WORDS][MAX_WORD_LENGTH])
//get all eligible second words from original start word
{
  char character;
  char currentword[MAX_WORD_LENGTH];
  int i;

  strcpy(currentword, words.startword);

  for (i = 0; currentword[i] != '\0'; i++)
  {
    strcpy(currentword, words.startword);
    for (character = 'a'; character <= 'z'; character++)
    {
      currentword[i] = character;
      strcpy(parentwords[i], currentword);
      printf("%s ", parentwords[i]);
    }
  }
  parentwords[i][0] = '\0';

  printf("\n\n");
}

void check_dictionary(char dictionary[MAX_WORDS][MAX_WORD_LENGTH],
    char parentwords[MAX_WORD_LENGTH][MAX_WORD_LENGTH])
//checks a generated word for eligibility against the dictionary, prints next generation words
{
  int i, j;

  printf("\nSecond words: \n\n");

  for (j = 0; parentwords[j][0] != '\0'; j++)
    ;
  {
    for (i = 0; dictionary[i][0] != '\0'; i++)
    {
      if ((strcmp(dictionary[i], parentwords[j])) == 0)
      {
        printf("%s \n", parentwords[j]);
      }
    }
  }
}

void usage(char * argv[])
//prints error message
{
  printf("Incorrect usage, try: ./program_name %s\n", argv[1]);
}

代码:

in_array

1 个答案:

答案 0 :(得分:1)

格式显示:

  for (j = 0; parentwords[j][0] != '\0'; j++)
;

大多数可能意味着:

 for (j = 0; parentwords[j][0] != '\0'; j++)

这里

  while (fscanf(file_name, "%s", dictionary[i++]) == 1)

使用i未初始化

因此,将其定义更改为包含初始化:

  int word_count = 0, i = 0;