Thread: HELP! Counting the Words Program

  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
    28,413
    Is this supposed to be a C or C++ program?
    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

  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
    28,413
    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.
    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

  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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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
    28,413
    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.
    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

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, 05:40 AM
  3. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM