2015-11-15 100 views
0

我在使用字符串和字符串數組以及正確使用strcpy時遇到了麻煩。我正在使用掃描的二維數組字典dictionary。然後我開始寫一個字,改變它的每一個字母來創建許多不同的變體,例如cat -> cbt, cct, cdt等。從那裏我將每個生成的單詞複製到一個二維數組中,並將這些生成的單詞與字典進行比較,以查看它們是否是真實的單詞。然後我想打印這些真實的單詞,即cat作爲開始單詞將生成bat,如果它在字典中,但是zat不會。當我運行代碼時,它會打印所有生成的單詞,但當它到達check_dictionary函數時,它不會打印任何單詞。字符串數組,strcpy和字符串的問題

的文本文件,它從讀取是這樣的:

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

縮進是殘暴...你應該做你自己的調試。如果'check_dictionary'不打印任何內容,那麼循環內的代碼很可能不會被執行。打印你用作條件的變量並查看原因。 – szczurcio

+0

這是爲什麼?變量不會縮進,循環會縮進一個製表符和+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; 
+1

所以當下面的(據說是?)嵌套'i'循環迭代時,'j'超出範圍。 –

+0

@WeatherVane ......並由此引發臭名昭着的未定義行爲,並從此開始任何事情發生。 – alk