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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:16:28+00:00 2026-06-15T23:16:28+00:00

I have done a program where input given should be stream of characters and

  • 0

I have done a program where input given should be stream of characters and program counts the
non-whitespace characters and words. word is defined as the stream of characters which are separated by a whitespace character. so here is the program..

#include <stdio.h> 
#include<ctype.h>
#include <stdbool.h>
#include<iso646.h>

int main(void)  
{   unsigned long int wordcount = 0,charcount = 0, count=1;
    int ch;
    bool flag, prev;



    while ((ch = getchar()) != EOF) 
      {   if(isgraph(ch))
              flag=true;
          else
              flag=false;

          if(flag)
             charcount++;

          if(count ==1)
             prev = flag;

        if(count != 1)
           {   if(prev and (not flag))
                  wordcount++;
               prev = flag;
           }

          count++;
      }

    if((ch == EOF) and flag)
          wordcount++;

   printf("\nnumber of words counted are %lu \n", wordcount);
   printf("\nnumber of characters counted are %lu \n", charcount);



   return 0;

}

now I have checked this program on simple sentences. But just for practice, I want to do
detailed software testing on this. So how can I do that ? Do I just give more number of sentences ? I tried to give few paragraphs from some novels I found at project gutenberg.
what else I can do here ? Also can I improve the efficiency of this program ?

  • 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-15T23:16:29+00:00Added an answer on June 15, 2026 at 11:16 pm

    There are various basic tests to do:

    1. Empty file
    2. File with one blank only
    3. File with one non-blank only
    4. File with one blank and one non-blank
    5. File with one non-blank and one blank
    6. File with multiple blanks only
    7. File with multiple non-blanks only
    8. File with multiple blanks followed by non-blanks

    And so it goes on…this is boundary testing; making sure that the code works correctly on boundary conditions.

    Your assignment of the value from getchar() to an unsigned long int (now fixed in the question) is unusual. Since the return value is positive for a regular character and negative (EOF) for end-of-file or error, it is normal to assign it to a signed plain int.

    Your test ch == EOF after the loop is redundant; the only way out of the loop is when the condition is true.

    Using <iso646.h> and the (macros) keywords and and not is unusual too.

    Most commonly, people do not put code on the same line as the open brace of a block.

    You could increment charcount in the if block where you set flag = true;. You could use an else block instead of if (count != 1). In fact, AFAICT, your code:

    if(count ==1)
        prev = flag;
    
    if(count != 1)
        {   if(prev and (not flag))
                wordcount++;
            prev = flag;
        }
    

    could be written as:

    if (count > 1 and prev and (not flag))
        wordcount++;
    prev = flag;
    

    The description ‘number of characters counted’ isn’t strictly accurate; it is the number of graphical (non-blank, non-control) characters that you’re reporting. That’s probably on the hyper-nitpicking end of the fussiness scale, though (along with the observation that the ‘number of words’ is a singular quantity and it should be ‘is’ rather than ‘are’).

    It is slightly unusual to start your count at 1 rather than zero. It seems to record ‘one more than the number of raw characters read into the program’, which is an unusual quantity to record. More normally, you’d initialize it to 0 too, and modify the test I rewrote to read:

    if (count != 0 and prev and (not flag))
    

    (You can use count != 0 or count > 0; for an unsigned value, the terms are equivalent.)

    You might be able to simplify your conditionals by initializing prev appropriately (probably to false).

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

Sidebar

Related Questions

I have done up a program which requires the following: prompts user input account
I have done one program that allow user can listen to the music via
Have you ever done any .net programming? Yes? Good, here's a massive broken program,
I have a program and as it's done now, it has a data directory
I almost done my coding for a specific program i have. I just need
I have two integer arrays created at runtime (size depends on the program input).
So I have a program where I ask the user to input an entry,
I have another task for my school and it is: Write a program which
Given a non-negative integer n and an arbitrary set of inequalities that are user-defined
Have done quite a bit of searching for a guide (of any substance) for

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.