Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Order of strings (http://www.go4expert.com/forums/order-of-strings-t3999/)

 gavin_55 23Apr2007 19:17

Order of strings

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.

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; }```

 DaWei 23Apr2007 19:55

Re: Order of strings

 gavin_55 23Apr2007 21:41

Re: Order of strings

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;}`

 shabbir 23Apr2007 21:43

Re: Order of strings

Offtopic comment:
Its just that I edited and you posted.

 DaWei 23Apr2007 23:32

Re: Order of strings

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.

 All times are GMT +5.5. The time now is 20:34.