[英]Problems with string arrays, strcpy and strings
I'm having real trouble working with strings and string arrays, and using strcpy correctly. 我在使用字符串和字符串数组以及正确使用strcpy时遇到了真正的麻烦。 I'm using a dictionary of words scanned in a 2D array dictionary
. 我正在使用在2D数组dictionary
扫描的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. 然后,我开始一个单词,改变它的每个字母以创建许多不同的变体,例如cat -> cbt, cct, cdt
等。从那里,我将每个生成的单词复制到2D数组中,并将这些生成的单词与字典进行比较看看它们是否是真实的单词。 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. 然后,我想打印这些实词,即,如果cat
在字典中作为起始词,它将生成bat
,但是zat
不会。 When I run the code it prints all the generated words but when It gets to check_dictionary
function it prints no words. 当我运行代码时,它会打印所有生成的单词,但是当到达check_dictionary
函数时,它不会打印任何单词。
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 i
未初始化使用
So change it definition to include an initialisation: 因此,将其定义更改为包括初始化:
int word_count = 0, i = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.