Thread: Need help with C program that count words, lines and characters

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    23

    Need help with C program that count words, lines and characters

    My C program doesn't seem to be working correctly, output is completely off . Basically the program is suppose to output amount of characters, amount of word and amount of newlines that have been typed. Multiple consecutive spaces should not be counted as multiple words. Any ideas?



    Code:
    #include <stdio.h>
    
    void main ()
    
    
    {
    	char c=0;
    	
    	int num_words=0, num_chars=0, num_newlines=0;
    
    
    	while ((c=getchar()) !=4);
    
    
    			num_chars++;
    			printf("Number of Characters: %d",num_chars);
    			
    		if (num_chars==10)//ASCII code of space
    
    
    		{
    			num_words++;
    			printf("Number of words: %d",num_words);
    		}
    		else (num_chars==32);
    		{
    			num_newlines++;
    			printf("Number of newlines: %d",num_newlines);
    		}
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    We write
    Code:
    int main(void)
    {
          ...
          return 0;
    }
    and not void, as you have written.

    Do not use magic numbers, like 4. Use EOF instead.
    You have
    Code:
    while ((c=getchar()) !=4);
    which is equivalent to this
    Code:
    while ((c=getchar()) !=4)
    {
          ;
    }
    thus it does not do nothing. Place your logic inside the body of the while, inside the brackets that is and try again
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    23
    I tried that. it still doesn't work.
    Need help with C program that count words, lines and characters-helloworld1-jpg
    here is my new code
    Code:
     #include <stdio.h>
    
    int main (void)
    
    
    {
    	char c=0;
    	
    	int num_words=0, num_chars=0, num_newlines=0;
    
    
    	while ((c=getchar()) !=4 && c!=EOF);
    	{
    
    
    			num_chars++;
    			printf("Number of Characters: %d\n",num_chars);
    			
    		if (num_chars==10)//ASCII code of space
    
    
    		{
    			num_words++;
    			printf("Number of words: %d\n",num_words);
    		}
    		else (num_chars==32);
    		{
    			num_newlines++;
    			printf("Number of newlines: %d\n",num_newlines);
    		}
    	}
    		return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First if you want to test c for EOF then it must be an int not a char.

    Second, please explain what you think is happening in the following line:

    Code:
    while ((c=getchar()) !=4 && c!=EOF);
    Also this line?
    Code:
            if (num_chars==10)//ASCII code of space
    First the decimal value for space is 32 not 10. Plus why are you trying to compare num_chars to a space? You really should stop using the magic numbers 10, 32 and use their character representations '\n' = new line and just use a space inside single quotes for the space character.

    Jim

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    23
    Second, please explain what you think is happening in the following line:
    Basically it's a while loop reading keys from the keyboard by using function getchar(). The reading of characters from keyboard will be stopped when ^D is typed which has ASCII value 4. I think EOF indicates the end of input. Do you think I should remove it?

    Code:
    while ((c=getchar()) !=4 && c!=EOF);
    Also this line?
    Yes it should be 32 not 10 for space. and it should be 10 for new line. Basically I am writing a cprogram that ask for input. The output of this program should be amount of characters, amount of works and amount of new lines.

    Code:
            if (num_chars==10)//ASCII code of space
    First the decimal value for space is 32 not 10. Plus why are you trying to compare num_chars to a space? You really should stop using the magic numbers 10, 32 and use their character representations '\n' = new line and just use a space inside single quotes for the space character.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The value of EOF does NOT necessary fit in a char datatype.
    "^D" has no real ASCII value; it is OS dependent. It will in I think Unix/Linux/Mac result in a EOF being generated.

    The most common value of EOF is -1; but, it can be any value. You must/should use int variable to ensure the value of EOF fits in the variable without its value being changed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    23
    here is my new code. It still doesn't count character/words/new line correctly
    Code:
    #include <stdio.h>
    
    int main (void)
    
    
    {
    	int c=0;
    	
    	int num_words=0, num_chars=0, num_newlines=0;
    
    
    	while ((c=getchar()) !=EOF);
    	{
    
    
    			num_chars++;
    			printf("Number of Characters: %d\n",num_chars);
    			
    		if (num_chars==' ')
    
    
    		{
    			num_words++;
    			printf("Number of words: %d\n",num_words);
    		}
    		else (num_chars=='\n');
    		{
    			num_newlines++;
    			printf("Number of newlines: %d\n",num_newlines);
    		}
    	}
    		return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you are comparing num_chars to various character literals, whereas I would expect that you want to compare c with them instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    23
    Tried that but still it doesn't seem to work correctly. Here the new code
    Code:
     #include <stdio.h>
    
    int main (void)
    
    
    {
        int c=0;
        
        int num_words=0, num_chars=0, num_newlines=0;
    
    
        while ((c=getchar()) !=EOF);
        {
    
    
                num_chars++;
                printf("Number of Characters: %d\n",num_chars);
                
            if (c==' ')
    
    
            {
                num_words++;
                printf("Number of words: %d\n",num_words);
            }
            else (num_chars=='\n');
            {
                num_newlines++;
                printf("Number of newlines: %d\n",num_newlines);
            }
        }
            return 0;
    }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Zach786 View Post
    Tried that but still it doesn't seem to work correctly.
    I do believe that.. What does your compiler tell ?
    Kurt

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong, firstly for the reason that I mentioned earlier, and also because of the misplaced semi-colon:
    Code:
    else (num_chars=='\n');
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    while ((c=getchar()) !=EOF);
    For a start you should eventually correct the error std10093 pointed out 18 hours ago.

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    May 2013
    Posts
    23
    [QUOTE=AndiPersti;1165792][CODE]
    while ((c=getchar()) !=EOF);
    So I have removed the semicolon. I can't remove the semicolon from else statement as laserlight suggested. I am getting compiler error. Here is new code
    Code:
     #include <stdio.h>
    
    int main (void)
    
    
    {
        int c=0;
        
        int num_words=0, num_chars=0, num_newlines=0;
    
    
        while ((c=getchar()) !=EOF)
        {
    
    
                num_chars++;
                printf("Number of Characters: %d\n",num_chars);
                
            if (c==' ')
    
    
            {
                num_words++;
                printf("Number of words: %d\n",num_words);
            }
            else (c=='\n');
            {
    
    
                    num_newlines++;
                printf("Number of newlines: %d\n",num_newlines);
            }
        }
            return 0;
    }
    Output:
    Need help with C program that count words, lines and characters-capture-jpg
    when I run the program. It works correctly for characters. how can i change the program so it counts the character and lines correctly? any help would greatly appreciate. Sorry if I am not seeing something obvious .

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What do you mean you can't remove the semicolon? Is your delete key broken?

    Perhaps now would be a good time to realise that you cant just plonk some expression after an "else", you must instead make it an "else if"!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 56
    Last Post: 04-26-2013, 03:13 PM
  2. Replies: 4
    Last Post: 06-16-2012, 12:51 PM
  3. Characters, words and lines counting
    By Darkobra in forum C Programming
    Replies: 4
    Last Post: 01-09-2012, 07:13 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM