#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 语句。问题是它跳过了告诉我该单词是否在数组中的部分。所以是的
根本原因: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]); /* 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); /* Updated: 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);
else
printf("%s is NOT in the array.\n", word);
}
}
return 0;
}