简体   繁体   中英

Problems with string arrays, strcpy and strings

I'm having real trouble working with strings and string arrays, and using strcpy correctly. I'm using a dictionary of words scanned in a 2D array dictionary . Then I take a start word, alter every letter of it to create many different variants, ie cat -> cbt, cct, cdt , etc. From there I copy each generated word into a 2D array and to compare these generated words to the dictionary to see if they are real words. I then want to print these real words, ie cat as a start word will generate bat if its in the dictionary, but zat won't be. When I run the code it prints all the generated words but when It gets to check_dictionary function it prints no words.

The text file it reads from is like:

mat
yes
cat
hat

The code:

#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]);
}

The formatting revealed this:

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

which most probably was meant to be:

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

Here

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

the i is used uninitialised

So change it definition to include an initialisation:

  int word_count = 0, i = 0;

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