Click here to Skip to main content
15,914,452 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: searching within strings Pin
David Crow26-Jul-06 9:18
David Crow26-Jul-06 9:18 
QuestionRe: searching within strings Pin
jon-8026-Jul-06 10:05
professionaljon-8026-Jul-06 10:05 
AnswerRe: searching within strings Pin
David Crow26-Jul-06 10:12
David Crow26-Jul-06 10:12 
AnswerRe: searching within strings [modified] Pin
Zac Howland26-Jul-06 10:17
Zac Howland26-Jul-06 10:17 
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 
jon_80 wrote:
for (int iCurrent = 0; iCurrent <= iNumberOfLines; iNumberOfLines++)
{

ptrToken = strstr(strSentence, strSearch);

while (ptrToken != 0 && *ptrToken != 0)
{ memcpy(strSentence, this->strSentence[iCurrent], MAX_SENTENCE_LENGTH);

if (strlen(ptrToken) > 0)
{ ++iWordCount;
ptrToken += strlen(strWord);
if (ptrToken >= strSentence + MAX_WORD_LENGTH)
{break;}
}
ptrToken = strstr(ptrToken, strSearch);
}
}



token = strstr(buffer, substring);
        while (token != 0 && *token != 0)
        {
                if (strlen(token) > 0)
                {
                        ++wordCount;
                        token += strlen(substring);
                        if (token >= buffer + MAX_WORD_LENGTH)
                        {
                                break;
                        }
                }
                token = strstr(token, substring);
        }


Your loop and mine are not the same, which is why you are having problems with it. Where is iNumberOfLines declared and initialized? Your outter for loop is still incrementing this value but checking it against the iterating value that doesn't change. After a while, you will find that token is invalid because you will have gone off into "never-never land" (part of memory that you shouldn't be in).

The function I posted will take any single character array and tell you how often a substring appears in it. If you want to do this for multiple lines, you would simple call that function in a loop:

char SomeWords[20][100] = { /* initialized to some set of strings */ };
char SearchString[10] = { /* initialized to some search string */ };
for (int i = 0; i < 20; ++i)
{
	unsigned long count = HowManyWords(SomeWords[i], SearchString);
	cout 	<< "Found substring (" << SearchString << ") " << count
		<< " times in string(" << i << ")." << endl;
}


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

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 
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 

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.