简体   繁体   English

解析C中的单词; 翻译计划

[英]Parsing words in C; Translating program

I'm developing a program that will translate a string from the user (English) into Spanish. 我正在开发一个程序,将用户(英语)的字符串翻译成西班牙语。 For the assignment I'm given a file that contains a list of a 100 words and their spanish equivalent. 对于作业,我给出了一个文件,其中包含100个单词的列表及其西班牙语等价物。 I've successfully opened that file, and fed it to the string with a two dimensional array. 我已成功打开该文件,并将其提供给具有二维数组的字符串。

What I'm having difficulty with is parsing the words so it will allow me to find the equivalent version of the given words; 我遇到的困难是解析单词,这样我就可以找到给定单词的等价版本; any words that aren't given are suppose to be replaced with asterisks (*). 任何未给出的单词都应该替换为星号(*)。 Any ideas on how I can parse the words from the users inputted string? 关于如何从用户输入的字符串中解析单词的任何想法? Below is snippits of the source code to save some time. 下面是源代码的snippits,以节省一些时间。

--Thanks - 谢谢

char readFile[100][25];

fp = fopen("words.dat", "r");

if (fp == NULL){
   printf ("File failed to load\n");
}

//This is how I stored the file into the two dimensional string.
while (fgets(readFile, 100, fp)){
   x++;
}

printf ("User please input string\n");
gets (input);

That's as far as I've gotten. 就我而言,这就是我的意思。 I commented out the for-loop that outputs the words so I can see the words (for the sake of curiousity) and it was successful. 我注释掉了输出单词的for循环,所以我可以看到单词(为了好奇)并且它是成功的。 The format of the file string is (english word), (spanish word). 文件串的格式是(英文单词),(西班牙语单词)。

First of, the array you declare is 100 arrays of 25-character arrays. 首先,您声明的数组是100个25个字符数组的数组。 If we talk about "lines" it means you have 100 lines where each line can be 24 characters (remember we need one extra for the terminating '\\0' character). 如果我们谈论“线”,它意味着你有100行,每行可以是24个字符(记住我们需要一个额外的终止'\\0'字符)。 If you want 25 lines of 99 characters each, switch place of the sizes. 如果您想要25行,每行99个字符,请切换大小的位置。

Secondly, you overwrite the same bytes of the array over and over again. 其次,一遍又一遍地覆盖数组的相同字节。 And since each sub-array is actually only 25 characters, you can overwrite up to four of those arrays with that fgets call. 由于每个子阵列实际上只有25个字符,因此您可以使用该fgets调用覆盖最多四个阵列。

I suggest something like this instead: 我建议这样的事情:

size_t count = 0;
for (int i = 0; i < sizeof(readFile) / sizeof(readFile[0]) &&
                fgets(readFile[i], sizeof(readFile[i]), fp); i++, count++)
{
}

This will make sure you don't read more than you can store, and automatically reads into the correct "line" in the array. 这将确保您读取的内容不会超出您的存储容量,并自动读入数组中正确的“行”。 After the loop count will contain the number of lines you read. 循环count将包含您读取的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM