By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

string searching

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Roberto Dias" <di*****@br.inter.net> wrote in message
news:51**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a
Roberto Dias wrote:
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.)

--
Regards,
Buster.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.