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

  • SEARCH
  • Home
  • 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 8967457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:14:51+00:00 2026-06-15T17:14:51+00:00

after numerous attempts to make my program work I turn to you guys. I

  • 0

after numerous attempts to make my program work I turn to you guys. I want to make a program that counts the numbers of times a specific word has been typed in. That specific word has been specified as an argument and I’m trying to recall it by using argc and argv. Then I want the program to count the number of times I type in a word and to finish I want to be able to type in #EOF so it stops and shows me the result. This is what I have been trying on so far.

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

int main(int argc, char *argv[])
{

int i=0;
char buf[1026]={'\0'};
char *p="#EOF\n";
fgets(buf, sizeof buf, stdin);

while((strcmp(buf, p) !=0) && (fgets(buf, sizeof buf, stdin) != NULL ))
{

   if(strncmp(buf, argv[1], strlen(argv[1])) == 0)
    {
        i++;
    }
}
printf("%d", i);

return 0;
}

I get no errors at all, but nothing really happens either. I did try to check where the fault lies, and I found out that when I try to display the value of I within the if statement it’s counting very very very fast!

Thanks in advance!

  • 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-15T17:14:53+00:00Added an answer on June 15, 2026 at 5:14 pm

    You have an infinite loop.

    fgets(buf, 1025, stdin);
    

    In the above line of code, fgets() will take from stdin a string of characters including the newline character and store it into buf.

    A simple fix to that is to add the newline character into the string you check against:

    char *p="#EOF\n";
    

    Other issues:

    1)

    char buf[1026]; // This should be initialized to an empty string when you start
                    // char buf[1026] = {'\0'}; would do it
    char *p="#EOF";
    
    while(strcmp(buf, p) !=0) 
    

    2) Because you used a while() loop, and you’re checking before getting any input, you’ll always enter it at least once. This might not be what you wanted to do. A do/while loop, or grabbing the input as the first step would be better.

    3) if(strncmp(buf, argv[0], this is checking against the name of your program, if you wanted to check against the first command line argument you wanted argv[1]

    4) If you’re trying to check that the first command line argument is the same as what the user is typing you want this:

    if(strncmp(buf, argv[1], strlen(argv[1])) == 0)
    

    My version of your code that’s working:

    int main(int argc, char *argv[])
    {
    
        int i=0;
        char buf[1026]={'\0'};
        char *p="#EOF\n";
        do
        {
           fgets(buf, 1025, stdin);
           if(strncmp(buf, argv[1], strlen(argv[1])) == 0)
                i++;
        } while (strcmp(buf, p) != 0);
        printf("%d\n", i);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After trying numerous methods to get watermarking to work for me, I finally found
I feel like this one should be easy but after numerous searches and attempts
I'm trying to translate an SQL query into LINQ, but after numerous attempts, I'm
I have numerous iframes that load specific content on my pages. Both the parent
After numerous projects one-way asynchronous message passing looks like the only way to write
After autheticating my user, I want to put a reference in the session to
It's a pretty classic problem. The company I work for has numerous business reports
After reading numerous posts on SO and other sites, I still cannot seem to
After experiencing numerous problems with a certain media player, it got me thinking is
A client still uses Visual SourceSafe, but after showing the numerous dangers and deficiencies

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.