Thread: A simpler way...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    A simpler way...

    For no particular reason I wrote an algorithm to count how many words in a string. I wrote it and it seemed pretty complicated for such a simple thing. What do you think?
    Code:
    // counts the words in a string by
    // counting the spaces in it
    
    #include <iostream.h>
    #include <string.h>
    
    int main ()
    {
    	char foo[500]; // buffer string
    	int spaces = 0; // amount of spaces
    	
    	cin.getline(foo, 500);
    	cout << endl;
    	
    	for(unsigned int i = 0; i < strlen(foo); i++)
    	{
    		if(foo[i] == ' ')
    		{
    			if(foo[i-1] != ' ')
    			{
    				
    				spaces++;
    			}
    		}
    	}
    	
    	if(foo[0] == ' ')
    	{
    		spaces--;
    	}
    	
    	for(unsigned int j = (strlen(foo) - 1); j > 0; j--)
    	{
    		if(foo[j] == ' ')
    		{
    			spaces--;
    		}
    		else
    		{
    			break;
    		}
    	}
    	
    	
    	
    	spaces += 1; // add one to make up for no space before first word
    	cout << "You typed " << spaces << " words\n";
    	
    	return 0;
    }

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    This is pretty rushed.. but it seems to work - and will even work around tabs spaces and commas - it is relatively easy to modify to work with other delimeters too.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    	char strbuff[256];
    	char seps[] = ",\t\n ";
    	char *tok;
    	int words = 0;
    
    	cout << "Enter a string: ";
    	cin.getline(strbuff, 255, '\n');
    
    	tok = strtok(strbuff, seps);
    
    	while (tok)
    	{
                    cout << tok << endl;
                    tok = strtok( NULL, seps );
                    words++;
    	}
    
    	cout << "The number of words are: " << words << endl;
    
    	return 0;
    }
    Last edited by foniks munkee; 11-28-2002 at 03:21 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int main ()
    {
    	string foo;
    
    	getline(cin,foo);
    	
    	cout << endl << "You typed ";
    	cout << count(foo.begin(),foo.end(),' ') + 1;
    	cout <<" words" << endl;
    }

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Oh god, that is so much simpler, Heh heh
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Fordy, there's a problem with your program.

    The amount of words isn't necissarily equal to the number of spaces plus one. What if someone uses two spaces? You have to account for that as well!

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Polymorphic OOP
    Fordy, there's a problem with your program.

    The amount of words isn't necissarily equal to the number of spaces plus one. What if someone uses two spaces? You have to account for that as well!

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    bool IsSpace(char c){
    	static char prev = '\0';
    	char ret = (c == ' ' && prev != ' ');
    	prev = c;	
    	return ret;	
    }
    
    int main ()
    {
    	string foo;
    	
    	getline(cin,foo);
    	
    	cout << endl << "You typed ";
    	cout << count_if(foo.begin(),foo.end(),IsSpace) + 1;
    	cout <<" words" << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bulky...can you make it simpler
    By freddyvorhees in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2008, 08:35 AM
  2. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 12:52 AM
  3. The simpler version of two programs doesn't work......
    By w00tw00tkab00t in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2006, 07:07 PM
  4. Array or Pointer (The Better Way?)
    By ajb268 in forum C++ Programming
    Replies: 15
    Last Post: 12-30-2004, 06:02 PM
  5. DirectPlay help...or maybe simpler?
    By Trireme in forum Windows Programming
    Replies: 0
    Last Post: 02-14-2003, 06:55 PM