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 9046245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:36:45+00:00 2026-06-16T11:36:45+00:00

I got a almost good working program to count words from standard input. The

  • 0

I got a almost good working program to count words from standard input.
The word what has to be count is a program argument.

The problem is that I use a white space to see a word but I also must count within th word itself.
Example: if my input is aa aaaa #EOF, and I want to count aa the result should be 4. My code result 2.

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


int word_cnt(const char *s, char *argv[])
{
    int cnt = 0;

    while(*s != '\0')
    {
        while(isspace(*s))
        ++s;
        if(*s != '\0')
        {
            if(strncmp(s, argv[1], strlen(argv[1])) == 0)
            ++cnt;

            while(!isspace(*s) && *s != '\0')
            ++s;
        }
     }

    return cnt;
}

int main(int argc, char *argv[])
{
    char buf[1026] = {'\0'};
    char *p="#EOF\n";
    int tellen = 0;

    if (argc != 2)
    {
        printf("Kan het programma niet uitvoeren, er is geen programma argument gevonden\n");
        exit(0);
    }

    while((strcmp(buf, p) !=0))
    {
        fgets (buf, 1025, stdin);
        tellen += word_cnt(buf, argv);
    }

    printf("%d", tellen);

    return 0;
}
  • 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-16T11:36:46+00:00Added an answer on June 16, 2026 at 11:36 am
    int word_cnt(const char *s, char *argv[])
    {
        int cnt = 0;
        int len = strlen(argv[1]);
        while(*s)
        {
                if(strncmp(s, argv[1], len) == 0)
                  ++cnt;
    
                ++s;
         }
    
        return cnt;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a RSA pubkey.dat (almost obvious what it is) that has the following
I've got a weird issue that almost seems like a Visual Studio 2008 issue.
I got this code from someone, it's almost perfect to create a dynamic breadcrumb,
I've got a strange problem here. I have a class that I'll call MainView
I've currently got a spreadsheet type program that keeps its data in an ArrayList
I've got a logging class which requires need to be called from almost everywhere
I'm dangerously close to launching a pretty heavy web app and I've got almost
I think I've almost got it, but not there quite... I want to select
I'm having some problems building an hierarchistic array structure - I have almost got
I've got a solution for my webapplication with almost 12 projects and 3 websites.

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.