提问人:Firdaus 提问时间:11/2/2023 最后编辑:Weather VaneFirdaus 更新时间:11/2/2023 访问量:42
如何使我的系统检测输入词是否在数组中
How do I make my system to detect if a input word is in an array
问:
#include <stdio.h>
#include <string.h>
//Detect Number Function
int hasNumbers(const char *word) {
int len = strlen(word);
for (int i = 0; i < len; i++) {
if (word[i] >= '0' && word[i] <= '9') {
return 1; // Found a digit
}
}
return 0; // No digits found
}
//Detect Symbol Function
int containsSymbol(const char *word) {
while (*word) {
if ((*word >= 'A' && *word <= 'Z') || (*word >= 'a' && *word <= 'z') || (*word >= '0' && *word <= '9') || *word == ' ') {
word++;
}
else {
return 1; // Symbol found
}
}
return 0; // No symbols found
}
//Detect Centre Letter
int containsLetterL(const char *word) {
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] == 'C' || word[i] == 'c') {
return 0; // 'L' or 'l' found
}
}
return 1; // 'L' or 'l' not found
}
//Detect Word in Array
int isWordInArray(char word[], char words[100][100], int wordCount) {
for (int i = 0; i < wordCount; i++) {
printf("CATS\n");
if (strcmp(word, words[i]) == 0) {
return 0; // Word is found in the array
}
printf("DOGS\n");
}
return 1; // Word is not found in the array
}
/*
int isWordInArray2(char word[100], char words[100][100], int wordCount) {
for (int i = 0; i < wordCount; i++) {
if (strcmp(word, words[i]) == 0) {
return 1; // Word is found in the array
}
}
return 0; // Word is not found in the array
}
*/
/*--------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------*/
int main () {
//Transfer Word in Text File to Array
FILE *file;
char wordA1[100]; // Assuming a maximum word length of 99 characters
char words_array[100][100]; // Assuming a maximum of 100 words in the file
// Open the file for reading
file = fopen("wordlist_Game1.txt", "r");
if (file == NULL) {
printf("File not found or cannot be opened.\n");
return 1;
}
int wordCountTextFile = 0;
// Read words from the file and store them in the array
while (fscanf(file, "%99s", wordA1) == 1) {
strcpy(words_array[wordCountTextFile], wordA1);
wordCountTextFile++;
}
// Close the file
fclose(file);
// Main Program
char word[100];
int wordCount = sizeof(word) / sizeof(words_array[0]);
while (1) {
printf("\nHere is your puzzle:\n");
printf(" C T U L H N A\n");
printf("Guess a word or END to quit: ");
scanf("%s", word);
printf("%s\n", word);
//Detect END
if (strcmp(word, "END") == 0) {
printf("You entered 'END.' Exiting the loop.\n");
break;
}
//Detect Number
else if (hasNumbers(word)) {
printf("The word contains numbers.\n");
}
//Detect Symbol
else if (containsSymbol(word)) {
printf("The word contains symbols.\n");
}
//Detect Centre Letter
else if (containsLetterL(word)) {
printf("The word doesn't contains centre letter 'C'.\n");
}
//Detect Word in Array
else if (isWordInArray(word, words_array, wordCount)) {
printf("%s is in the array.\n", word);
}
/*
//Detect Word in Array
else if (isWordInArray2(word, words_array, wordCount)) {
printf("%s is NOT the array.\n", word);
}
*/
}
}
此代码用于检测遵循这些规则的特定单词。除了 isWordInArray 函数之外,每个函数都工作正常。此函数用于检测数组中的单词 if。我希望它告诉我单词是否不是数组并重复循环。如果单词在数组中,则系统将继续 if-else 语句。问题是它跳过了它告诉我单词是否在数组中的部分。所以是的
答:
0赞
greg spears
11/2/2023
#1
根本原因:isWordInArray() 由于多种原因无法正确执行:
- isWordInArray() 在找到单词时返回 false(0),在未找到单词时返回 true(1)
- 代码将“wordCount”作为参数传递给 isWordInArray(),但“wordCount”的计算结果为 1 - 而不是实际的字数。因此,从未解析过完整列表。我们通过将 wordCountTextFile 传递给 isWordInArray() 来解决这个问题。我们从读取操作中获得了实际的字数,那么为什么不使用它呢?
下面是应用了修复和注释的可编译、可工作代码子集:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*---------------------------------------------------------------------------------
isWordInArray()
----------------------------------------------------------------------------------*/
int isWordInArray(char word[], char words[100][100], int wordCount)
{
int i;
/* Updated: Added some troubleshooting print outs */
printf("isWordInArray() reports list -- wordcount = %d\n", wordCount);
for(i = 0; i < wordCount; i++)
printf("%d: %s\n", i, words[i]);
for(i = 0; i < wordCount; i++)
{
if(strcmp(word, words[i]) == 0)
{
return 1; /* Updated to 1 ( Word is found in the array) */
}
}
return 0; /* Updated to 0 ( Word is NOT found in the array) */
}
int main()
{
//Transfer Word in Text File to Array
FILE *file;
char wordA1[100];
char words_array[100][100];
int wordCountTextFile = 0;
char word[100];
int wordCount = sizeof(word) / sizeof(words_array[0]); /* NOTE!: This evaluates to 1 */
file = fopen("wordlist_Game1.txt", "r");
if (file == NULL) {
printf("File not found or cannot be opened.\n");
return 1;
}
printf("wordCount: %d\n", wordCount); /* Added: This will print "1" */
while (fscanf(file, "%99s", wordA1) == 1)
{
strcpy(words_array[wordCountTextFile], wordA1);
wordCountTextFile++;
}
fclose(file);
// Main Program
while (1)
{
printf("\nHere is your puzzle:\n");
printf(" C T U L H N A\n");
printf("Guess a word or 'q' to quit: ");
scanf("%s", word);
printf("%s\n", word);
//Detect END
if (strcmp(word, "END") == 0)
{
printf("You entered 'END' Exiting the loop.\n");
break;
}
//Detect Word in Array
else
{
/* Update: param wordCountTextFile instead of wordCount*/
if (isWordInArray(word, words_array, wordCountTextFile))
printf("%s IS in the array!!!\n", word); /* Updated */
else
printf("%s is NOT in the array.\n", word); /* Added */
}
}
return 0;
}
评论
isalnum()
containsLetterL()
C
0