Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:57:49+00:00 2026-06-14T15:57:49+00:00

I have this following piece of code to check an input against a dictionary

  • 0

I have this following piece of code to check an input against a dictionary of common words and also check if input matches previous input stored in the passHistory file.My problem is that the strcmp method to compare strings in C doesn’t seem to execute correctly in my code as it fails to show appropriate error if common word used or input was already used in passHistory.

Some guidance would be appreciated.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX 30
#define gC_FOUND 99
#define gC_NOT_FOUND -99


int checkWordInFile(char * fileName,char * theWord);



int main()
{

    char userString[MAX + 1];

    int iResult;

    printf("Enter your string: ");
    gets(userString);


    printf("\n\nYou entered: %s, please wait, checking in dictionary.\n\n", userString);
    iResult = checkWordInFile("dictionary.txt",userString);




    if( iResult == gC_FOUND )
    {
        printf("\nFound your word in the dictionary");
    }
    else
    {
        printf("\nCould not find your word in the dictionary");
    }

    iResult = checkWordInFile("passHistory.txt",userString);
    if( iResult == gC_FOUND )
    {
        printf("\nPassword used");
    }
    else
    {
        printf("\nOk to use!");
    }

    printf("\n\n\n");
    system("pause");

} /* end of main */

int checkWordInFile(char * fileName,char * theWord){

    FILE * fptr;
    char fileString[MAX + 1];
    int iFound = -99;
    //open the file
    fptr = fopen(fileName, "r");
    if (fptr == NULL)
    {
        printf("\nNo dictionary file\n");
        printf("\n\n\n");
        system("pause");
        return (0); // just exit the program
    }

    /* read the contents of the file */
    while( fgets(fileString, MAX, fptr) )
    {
        if( 0 == strcmp(theWord, fileString) )
        {
            iFound = -99;
        }
    }

    fclose(fptr);

    return(0);



}//end of checkwORDiNFile
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:57:50+00:00Added an answer on June 14, 2026 at 3:57 pm

    fgets() writes the new-line character, if encountered, into the buffer it is populating. Remove it before using strcmp():

    char* new_line = strrchr(fileString, '\n');
    if (new_line) *new_line = 0;
    

    Note that gets() is a dangerous api due to no bounds checking on the input, potentially resulting in a buffer overrun. A safer mechanism for reading the user input would be fgets() or scanf() with the %Ns specifier where N specifies the maximum number of characters to read, and N must be one less than the size of the array to allow for the null terminator:

    scanf("%30s", userString);
    

    When the string is found in the file there is no reason to keep searching the remainder of the file, break from the while to avoid unnecessary processing. Note that iFounds value never changes within the checkWordInFile() and is not used as the return value: 0 is always returned. I think you meant iFound = gC_FOUND; within the loop. You have also defined macros to indicate found and not found but do not use these within the function, but use the hard-coded values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this MFC app with the following piece of code to check for
I have the following piece of code from a book. There is this function
I have this following piece of code: all_messages = {} num = None index
I have this following piece of code, which loads UIWebView properly on one Wifi
I have this following jquery text fly-in animation.Here is my code before I explain
I have this following code, here CssClass is of the String type. <asp:Panel ID=Panel1
I have this following code which will return all the current semesters. How do
I have written the following piece of code if( (!isset($_SESSION['home'])) || (!isset($_SESSION['away'])) ) I
I already have this piece of functioning code, but after writing it I did
I use the following piece of code to check out and then check in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.