string searching

R

Roberto Dias

Hi all,

What to do for searching for more than one string occurrence in the
same string (this last are line of a text). I have used getline(), to
get the text lines by means of WHILE loop and find(), to searching.
When find() finds a first occurrence it scape and don't detect
possible other one in the same string.
Here is a fragment:

***************************************************************************
cout << " " << endl;
cout << "Pesquisar pelo string: " << endl;
cin >> consulta;

comp = consulta.length();

while(ArquivoOrigem.getline(LinhaLidaArray, 80))
{

linha++;

LinhaLidaString = LinhaLidaArray;

if (LinhaLidaString.find(consulta) != string::npos)

{
cout<< linha
<< " - "
<< (LinhaLidaString.find(consulta)+1)
<< " a "
<< (LinhaLidaString.find(consulta)+1)+(comp-1)
<< "." << endl;

++ocorrencias;
}

}

***********************************************************************

thanks,

Roberto Dias
 
M

Mike Wahler

Roberto Dias said:
Hi all,

What to do for searching for more than one string occurrence in the
same string (this last are line of a text). I have used getline(), to
get the text lines by means of WHILE loop and find(), to searching.
When find() finds a first occurrence it scape and don't detect
possible other one in the same string.
Here is a fragment:

***************************************************************************
cout << " " << endl;
cout << "Pesquisar pelo string: " << endl;
cin >> consulta;

comp = consulta.length();

while(ArquivoOrigem.getline(LinhaLidaArray, 80))
{

linha++;

LinhaLidaString = LinhaLidaArray;

if (LinhaLidaString.find(consulta) != string::npos)

{
cout<< linha
<< " - "
<< (LinhaLidaString.find(consulta)+1)
<< " a "
<< (LinhaLidaString.find(consulta)+1)+(comp-1)
<< "." << endl;

++ocorrencias;
}

}

#include <iostream>
#include <string>

void multifind(const std::string& text, const std::string& to_find)
{
std::string::size_type pos(0);
const std::string::size_type len(to_find.size());
bool found(false);

std::cout << "string to search:\n" << text << "\n\n"
<< "searching for sequence: " << to_find << '\n';

while((pos = text.find(to_find, pos)) != std::string::npos)
{
found = true;
std::cout << "found at offset " << pos << '\n';
pos += len;
}

if(!found)
std::cout << "not found\n";

std::cout << '\n';
}

int main()
{
std::string s("This is a sentence.");
multifind(s, "is");
multifind(s, "en");
multifind(s, ".");
multifind(s, "sentence");
multifind(s, "hello");
return 0;
}

-Mike
 
B

Buster

Roberto said:
Hi all,

What to do for searching for more than one string occurrence in the
same string (this last are line of a text). I have used getline(), to
get the text lines by means of WHILE loop and find(), to searching.
When find() finds a first occurrence it scape and don't detect
possible other one in the same string.
Here is a fragment:

***************************************************************************
cout << " " << endl;
cout << "Pesquisar pelo string: " << endl;
cin >> consulta;

comp = consulta.length();
while(ArquivoOrigem.getline(LinhaLidaArray, 80))
{

linha++;

LinhaLidaString = LinhaLidaArray;

You should use std::getline instead of the getline member. Then
arbitrary-length lines will be handled correctly.

while (std::getline (ArquivoOrigem, LinhaLidaString))
{
++ linha;
if (LinhaLidaString.find(consulta) != string::npos)

{
cout<< linha
<< " - "
<< (LinhaLidaString.find(consulta)+1)
<< " a "
<< (LinhaLidaString.find(consulta)+1)+(comp-1)
<< "." << endl;

++ocorrencias;
}

for (std::string::size_type n = LinhaLidaString.find (consulta, 0);
n != std::string::npos;
n = LinhaLidaString.find (consulta, n + consulta.length ()))
{
std::cout << "...\n";
++ ocorrencias;
}

(If you want to find overlapping matches, put "n + 1" instead of
"n + consulta.length ()" in the for-condition.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top