HELP! Counting the Words Program

This is a discussion on HELP! Counting the Words Program within the C++ Programming forums, part of the General Programming Boards category; Ok so i've been messing around with this program for many hours and I still dont know how to make ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    Question HELP! Counting the Words Program

    Ok so i've been messing around with this program for many hours and I still dont know how to make it work.

    The program is meant to count the words in the string, "Which I will change to make it so it counts the words the user inputs." I thought I was on the right track and hopefully I was.

    If you can help me out I will be greatly thankful.

    Here is what I have so far.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    	char string1[20]="Count words please";
    
    	int x=0,y=strlen(string1),z=0;
    
    	printf("String Length is %d\n\n",y);
    
    	for(string1[x];x<y;x++)
    	{
    		if(string1[x]==NULL)
    		{
    			z ++;
    		}
    		printf("%c",string1[x]);
    		
    	}
    	printf("\n\nThere are %d word(s)",z);
    	scanf("%*c");
    
    }
    And once again thanks for the help!!!!!!!

    As you can tell im a beginner

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Is this supposed to be a C or C++ program?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Is this supposed to be a C or C++ program?
    It is C++

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Mrjunkie View Post
    It is C++
    That's not C++, it is C!

    Back to your original question, if I was doing this in C++, the easiest way would be to count the words as they are read in.

    So set up a counter that each time a word is read, the counter is incremented until end of file.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Since this is C++, my recommendation is to switch from using C-style I/O to C++-style I/O, i.e., #include <iostream> instead of <stdio.h>. Take advantage of std::string, and thus #include <string> instead of <string.h>.

    Next, to actually count the words, you could #include <map> and use a std::map<std::string, std::size_t>. The idea is that as you read in a word, you increment the corresponding map entry. When you are done, the map contains the mapping of words to their counts.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Since this is C++, my recommendation is to switch from using C-style I/O to C++-style I/O, i.e., #include <iostream> instead of <stdio.h>. Take advantage of std::string, and thus #include <string> instead of <string.h>.

    Next, to actually count the words, you could #include <map> and use a std::map<std::string, std::size_t>. The idea is that as you read in a word, you increment the corresponding map entry. When you are done, the map contains the mapping of words to their counts.
    I think he just wants to count the number of total words, rather than individual instances of an arbitrary word. Also, I think maps are a little advanced for his level just yet.

    I would simply set up a counter that increments each time a word is read, et viola!!

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Quote Originally Posted by darren78 View Post
    I think he just wants to count the number of total words, rather than individual instances of an arbitrary word. Also, I think maps are a little advanced for his level just yet.

    I would simply set up a counter that increments each time a word is read, et viola!!
    Yeah I did something like that here.
    I was told when a character that has nothing in it equals 0 or NULL, so I made that little if statement to look for that NULL and then add 1 to z "counting the word" and it just doesnt work =[.
    If that isnt what you meant then I dont understand.
    Code:
    	
    for(string1[x];x<y;x++)
    {
    	if(string1[x]==NULL)
    	{
    		z ++;
    	}
    	printf("%c",string1[x]);
    		
    }

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Mrjunkie View Post
    Yeah I did something like that here.
    I was told when a character that has nothing in it equals 0 or NULL, so I made that little if statement to look for that NULL and then add 1 to z "counting the word" and it just doesnt work =[.
    If that isnt what you meant then I dont understand.
    Code:
    	
    for(string1[x];x<y;x++)
    {
    	if(string1[x]==NULL)
    	{
    		z ++;
    	}
    	printf("%c",string1[x]);
    		
    }
    okay, using C++, it would look something like this:

    Code:
    
    
    int counter = 0;
    string temp;
    while(cin>>temp)
             ++counter;
    And thats pretty much the full solution to what you want to do, bar printing out the counter.

    How are you learning C++? As Laserlight and I said in a previous post, you are using mainly C style coding here and not 'proper' C++.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Also, I think maps are a little advanced for his level just yet.
    Then it is a great time to learn about them. Laserlight is correct in that a map makes this task very easy to do.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    If it really is just counting the number of "words" rather than the frequency of each distinct "word", then yeah, darren78's simple counting suggestion, not a map, is the way to go.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with number counting program
    By Turtal in forum C Programming
    Replies: 11
    Last Post: 04-25-2009, 02:40 PM
  2. Replies: 2
    Last Post: 12-02-2007, 04:40 AM
  3. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21