![]() |
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.
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 |
Re: Order of strings
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.
|
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
|
Re: Order of strings
Offtopic comment:
Its just that I edited and you posted. |
Re: Order of strings
First of all, silly or obvious comments are worthless.
Code:
int getWords (char *ptr2)//FUNCTION no kidding!Code:
count < LENGTH what, exactly, is LENGTH, and from whence does it come?Code:
if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'SCode:
//FIND NEXT SPACEFurther, inappropriately named functions are misleading. Code:
int getWords (char *ptr2)//FUNCTIONIn 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. |
| All times are GMT +5.5. The time now is 10:55. |