Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching character by character

Status
Not open for further replies.

londonDevil

Programmer
Nov 7, 2002
7
GB
How do I search through arrays character by charcter looking for spaces in order to count the words ?

I have gone as far as saving the info from two seperate windows in two different arrays, on selecting a menu option 'Save' as can be seen from the code below:

......

case IDM_SAVE:

GetWindowText(hWndSmall, Module[index].szTitle, TITLESIZE);
GetWindowText(hWndBig, Module[index].szAbstract, ABSTRACTSIZE);
index++;

.....

Hope 1 of you can shine a light on this.
Thanks! Really Appreciated!
 
Character by character, I think you can just say:

int count = 0;
int i = 0;

while( myCharArray != EOF )
if ( myCharArray[i++] == ' ' )
++count;

if that doesn't work, try substituting '\n' for EOF. Word of warning, though. If there are two spaces between words, or a space at the end of the sentence, the above count will be off.

There should be some other word count function somewhere I imagine (relating to tokenizing?), someone else should know that.
 
Thanks for all your help!! Much Much Appreciated!!
I've made some changes and finally want to
display the final count of the variable 'lNumOfWords' this in the Stats Window:


I decided to use this search method for the array:

case IDM_Stats:
{
long lNumOfWords= 0;
for(long i=strlen(Module[displayIndex].szTitle) - 1; i >= 0; i--)
{
if(Module[displayIndex].szTitle == ' ')
{
lNumOfWords++;

TextOut( hDC,3,10, Module[displayIndex].szTitle, lNumOfWords);

}
}

The 'TextOut' method was my attempt at getting the info but it didnt work and am not sure How to make the TextOut method work?

Thanks SO much Again!
 
//** You also might want to use the STL.

bool is_WS(char); // prototype. to test for White Space.

char Module[] = "Whatever is in your string";

std::vector< char > v( Module, strlen(Module));

// ... you could input your string into your new vector too.
// ... investigate the istream_iterator ... start with
std::istream_iterator< char > inputChar ( cin );
// ...

int result = std::count(v.begin(), v.end(), ' ');
cout << &quot;number of words: &quot; << result << endl;

// or ...
int result = std::count_if(v.begin(), v.end(), is_WS);
cout << &quot;number of words: &quot; << result << endl;

// ...

bool is_WS(char value)
{
// code to test current char value for White Space.
// return true or false
}

I have not tested this sample, but it should be &quot;close&quot;
Good luck, ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top