Click here to Skip to main content
15,912,507 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: searching within strings Pin
jon-8027-Jul-06 7:46
professionaljon-8027-Jul-06 7:46 
GeneralRe: searching within strings Pin
Zac Howland28-Jul-06 3:38
Zac Howland28-Jul-06 3:38 
GeneralRe: searching within strings Pin
jon-8028-Jul-06 8:08
professionaljon-8028-Jul-06 8:08 
GeneralRe: searching within strings Pin
Zac Howland28-Jul-06 9:05
Zac Howland28-Jul-06 9:05 
GeneralRe: searching within strings Pin
jon-8028-Jul-06 22:18
professionaljon-8028-Jul-06 22:18 
AnswerRe: searching within strings Pin
jon-8028-Jul-06 22:53
professionaljon-8028-Jul-06 22:53 
GeneralRe: searching within strings Pin
jon-802-Aug-06 10:07
professionaljon-802-Aug-06 10:07 
AnswerRe: searching within strings Pin
Zac Howland26-Jul-06 9:13
Zac Howland26-Jul-06 9:13 
jon_80 wrote:
char *strCurrentSentence;
ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
char *ptrPosition;


These statements are unnecessary. You could have just declared your pointers as follows:

char *strCurrentSentence = NULL;<br />
char *ptrPosition = NULL;


Calling ZeroMemory on a pointer like this does nothing but waste CPU cycles by jumping to the function in memory.

jon_80 wrote:
for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{ strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);

}


for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{
strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);
}

There are a several problems here:

1) Where is iNumberOfLines declared and initialized?
2) Doing a loop on an increasing number of iterations (that is, the number of iterations changes as the loop continues) is asking for trouble.
3) You cannot use strcpy to copy the string to strCurrentSentence. strCurrentSentence is a pointer to a character that has no memory allocated for it. I believe what you want to do is: strCurrentSentence = &strSentence[iCurrent];
4) iCurrent is never changed, so chances are this will become an infinite loop

Just a recommendation: Unless you have substrings that you are looking for, try making a copy of the string and using strtok to count the words, or simply doing a split on punctuation and whitespace.

A simple algorithm for the last part would be something like:

1) Split input string on ' ', ',', '.', '!', '?', ';', ':' and store resulting words in a list or vector
2) Call .size() on the list or vector to get word count.

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

QuestionHow to write to DataBase MS Access Pin
nahitan26-Jul-06 7:28
nahitan26-Jul-06 7:28 
AnswerRe: How to write to DataBase MS Access Pin
Hamid_RT26-Jul-06 7:36
Hamid_RT26-Jul-06 7:36 
GeneralRe: How to write to DataBase MS Access Pin
nahitan26-Jul-06 8:24
nahitan26-Jul-06 8:24 
QuestionRe: How to write to DataBase MS Access Pin
David Crow26-Jul-06 8:16
David Crow26-Jul-06 8:16 
AnswerRe: How to write to DataBase MS Access Pin
led mike26-Jul-06 8:41
led mike26-Jul-06 8:41 
QuestionWhy doesn't this work? Simple file I/O Pin
liuphil126-Jul-06 7:25
liuphil126-Jul-06 7:25 
AnswerRe: Why doesn't this work? Simple file I/O Pin
Zac Howland26-Jul-06 7:47
Zac Howland26-Jul-06 7:47 
GeneralRe: Why doesn't this work? Simple file I/O Pin
liuphil126-Jul-06 8:01
liuphil126-Jul-06 8:01 
GeneralRe: Why doesn't this work? Simple file I/O Pin
David Crow26-Jul-06 8:18
David Crow26-Jul-06 8:18 
GeneralRe: Why doesn't this work? Simple file I/O Pin
liuphil126-Jul-06 8:52
liuphil126-Jul-06 8:52 
GeneralRe: Why doesn't this work? Simple file I/O Pin
Zac Howland26-Jul-06 8:57
Zac Howland26-Jul-06 8:57 
GeneralRe: Why doesn't this work? Simple file I/O Pin
Zac Howland26-Jul-06 8:56
Zac Howland26-Jul-06 8:56 
Questionform Displaying Pin
ningthemcha26-Jul-06 6:46
ningthemcha26-Jul-06 6:46 
AnswerRe: form Displaying Pin
Chris Losinger26-Jul-06 7:04
professionalChris Losinger26-Jul-06 7:04 
AnswerRe: form Displaying Pin
David Crow26-Jul-06 7:06
David Crow26-Jul-06 7:06 
AnswerRe: form Displaying Pin
Zac Howland26-Jul-06 7:28
Zac Howland26-Jul-06 7:28 
AnswerRe: form Displaying Pin
Hamid_RT26-Jul-06 7:29
Hamid_RT26-Jul-06 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.