Thread: Ideas?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Ideas?

    Hello,
    I am needing to accept a string of characters from the user, count the number of words, and the average number of letters per word. I've got it about half way done, but feel I'm going in the wrong direction. Any suggestions would be appreciated.
    Thanks,
    George
    Code:
    #include <iostream.h>
    #include <string>
    using namespace std;
    
    int wordCount(int *ptrStr);	//Function prototype
    
    int main(void)
    {
    	char number[81];
    	int average;
    	string size;
    	int *ptrString;
    
    	cout<<"Please enter a string, 80 characters or fewer: "<<endl;
    	cin.getline(number,81);
    	*ptrString=int (number);
    	
    	size=number;
    	cout<<size.length()<<endl;
    	wordCount(ptrString);
    	
    	average=int(wordCount(ptrString)) / size;
    	cout<<number<<" "<<wordCount(ptrString);
    	return 0;
    
    }
    int wordCount(int *ptrString)
    {
    	int index=0;
    	int word=0;
    	while (index<*ptrString)
    	{
    		index++;
    			
    	if (ptrString[index] == ' ')
    		ptrString[index]= '\0';
    		word++;
    
    	} 
    	
    	return  word; 
    }
    Last edited by George; 02-15-2003 at 04:10 PM.

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Post your code so that we can see if you're going in the "wrong direction".. if such a thing exists. Remember to use code tags too.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'd traverse the string character by character.
    If you hit a character, start counting until you find a space, then you store that counter-number (which is the length of the word). Also, increase the word-counter by 1.
    Continue traversing until you find a non-space, then repeat everything again.
    At the end of the string, calculate the mean value of all counter values you stored.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  3. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  4. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM