Order of strings

gavin_55's Avatar, Join Date: Apr 2007
Newbie Member
Hi there, i have a small program in C++ that counts how many words are in a sentence. I understand the steps it takes to do this but am having difficulty get those steps in code.

i then need to discard all leading zero's.
find a character using isalpha()
increment counter.
find next zero and start over.

This is what i have so far.

Code:
int getWords (char *ptr2)//FUNCTION
{
	int sum = 0;
	bool check = false;//FLAG
	while (!check)//WHILE LOOP
		for (int count = 0; count < LENGTH; count++)//COUNTER
		{
			if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'S
			{
				//DO NOTHING
			}
			if (isalpha(ptr2[count]))//FIND CHARACTER
			{
				sum++; //BEGINNING OF A WORD, START COUNTER
				for (int num = count; num < LENGTH; num++)
				{
					//FIND NEXT SPACE
					if (!isalpha(ptr2[num]))
					{
						check = false;
						count = num;
					}
					else 
						check = true;
				}
			}
		}
			
		return sum;
}

Last edited by shabbir; 23Apr2007 at 21:43.. Reason: Code block
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Please read the "Before you make a query" thread, so we don't have to look at ugly, unformatted code in order to help you.
0
gavin_55's Avatar, Join Date: Apr 2007
Newbie Member
I'm sorry, i didn't know i could tag them like this.
Here is my code, its about as clean as it gets for me. I apologise again.

Code: cpp
int getWords (char *ptr2)//FUNCTION
{
    int sum = 0;
    bool check = false;//FLAG
    while (!check)//WHILE LOOP
        for (int count = 0; count < LENGTH; count++)//COUNTER
        {
            if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'S
            {
                //DO NOTHING
            }
            if (isalpha(ptr2[count]))//FIND CHARACTER
            {
                sum++; //BEGINNING OF A WORD, START COUNTER
                for (int num = count; num < LENGTH; num++)
                {
                    //FIND NEXT SPACE
                    if (!isalpha(ptr2[num]))
                    {
                        check = false;
                        count = num;
                    }
                    else
                        check = true;
                }
            }
        }
           
        return sum;
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Offtopic comment:
Its just that I edited and you posted.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
First of all, silly or obvious comments are worthless.
Code:
int getWords (char *ptr2)//FUNCTION no kidding!
while (!check)//WHILE LOOP really!
Secondly, the use of things which have not been clearly shown may suggest that there is an uninitialized variable, when in fact everything might be fine. My crystal is in the shop for 'ball' joints.
Code:
 count < LENGTH what, exactly, is LENGTH, and from whence does it come?
Thirdly, erroneous comments are as bad as useless comments. You imply that words are separated by zeroes. Perhaps you mean non-alphabetic characters?
Code:
if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'S
and
Code:
                    //FIND NEXT SPACE
                    if (!isalpha(ptr2[num]))
                    {
                        check = false;
                        count = num;
                    }
perhaps you meant "find next non-alphabetic character"?

Further, inappropriately named functions are misleading.
Code:
int getWords (char *ptr2)//FUNCTION
Isn't this really "getWordCount"??? "getWords" would return the words.

In any event, you should pass the function a string, as in the C definition of string, so that the maximum length to work with can be determined by strlen or by the occurrence of a nul character, OR (this is C++, right, says "bool"?) use the C++ string class, which carries the length as a member.

Here's an untested snippet, suitable for a C string, you might consider.
Code:
while (*ptr && !isalpha (ptr++);  Note the semicolon; this loop does nothing but find alpha or end.
if (*ptr) count++; Word found, since not pointing at nul.
while (*ptr && isalpha (ptr++));  Nothing but find end of alpha (thus, one word)
Wash, rinse, and repeat until nul found, return the count.