473,498 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string searching

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2485
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
10
39323
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
6
1831
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
3
2079
by: Paul H | last post by:
I have a text file that contains the following: ******************** __StartCustomerID_41 Name: Fred Smith Address: 57 Pew Road Croydon
8
11312
by: MM | last post by:
Hello there I need a way to efficiently check if a string I specify can be found in another string. For example, if I look for the string "searchString" in the string "jlh ahs dalskd" then it...
18
1691
by: Toto | last post by:
Hello, my problem is quite simple to explain. I have the following string: "table+camera" and I want to remove the + sign: "tablecamera". How do i do that ?
32
14760
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
3310
by: bushwacker | last post by:
Hello all, I'm a chemical engineering student. our teacher has given us a project to do some calculations based on some equations. those equations include constants, which are to be read from a...
5
3788
by: fminervino | last post by:
Hi friends !! I'm neophite about python, my target is to create a programa that find a specific string in text file. How can do it? Thanks fel
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.