Sign in to follow this  

C++ String Search

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, here we go. I have borland c++ 6...and im wondering how to search through all parts of a string. for example you have these AnsiString names[2] = {"Save A Horse Ride A Cowboy", "Rednecks"}; haha, making fun of rednecks... anyways, i would like to know a way to have another variable, maybe ansistring that the user gives you, and then have that as terms to search through the names. So for example if i type "Ride", it would give me the first song and if i type "necks" it would give me the second song. I would like it to search through the whole string to see if what the user of my program typed in is in any part of that string. could anyone help me please...

Share this post


Link to post
Share on other sites
Use std::string::find().

a.find(b), where a is a std::string and b is a character, C-string or std::string will return std::string::npos if b isn't a sub-string of a, and will return the offset at which b is found in a otherwise.

Share this post


Link to post
Share on other sites

std::string song = "Save a Horse Ride a Cowboy";

std::string search = "Ride";

if (song.find(search) != std::string::npos) cout << "Found search inside song!";


Of course, if search was "ride" instead, it wouldn't work. Best bet is to convert everything to uppercase, and then convert the input to uppercase too.

Share this post


Link to post
Share on other sites


#include <string>
#include <iostream>

using namespace std;

int main()
{
string myString = "A Man, A Plan, A Canal - Panama!";
string searchString = "Plan";

int pos = myString.find(searchString);

if( pos != string::npos ) // should reall use string::npos here (Washu: and now you do!)
{
// found it
cout << "Found 'Plan' at Char " << pos << endl;
}
else {
cout << "Couldn't find 'Plan'";
}
}




Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
LOL, nice Washu, but i used -1 originaly because on my
Visual Studio 7.1 2003 (right out of the Box) it didnt work
and kept saying npos was not a member of string::

strange, huh?

Hrm, works for me. Might try pos.npos

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the declaraction string myString = "blah"; doesnt work...it says that it has a declaration syntax error....

im doing this in visual c++


//---------------------------------------------------------------------------

#include
#include
#include
#pragma hdrstop

#include "Unit1.h"
string song = "Save A Horse, Ride A Cowboy";
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1 -> Items -> Add(song);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
string search = Edit1 -> Text;
int pos = song.find(search);
if(pos != string::npos){
ListBox1 -> Items -> Add("Did Find");
}
else {
ListBox1 -> Items -> Add("Did Not Find");
}
}
//---------------------------------------------------------------------------



lots of errors in that...

[Edited by - Washu on September 13, 2004 11:20:56 AM]

Share this post


Link to post
Share on other sites
Wait a minute.... thats C++ Builder Code, you cant
do that in Visual C++. For VC, look into MFC or WTL with
the CString class.

Search Google on some tutorials, you seem a little lost about
Windows Programming :)

but if you wanted to use [b]Borlan C++ Builder 3.0+[b] and that
msvc thing was a typo


//---------------------------------------------------------------------------

#include
#include
#include
#pragma hdrstop

#include "Unit1.h"

// CHANGED TO ANSISTRING FOR C++ BUILDER
AnsiString song("Save A Horse, Ride A Cowboy");

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1 -> Items -> Add(song);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString search = Edit->Text; // Use AnsiString now
int Pos = song.Pos( search );

if( Pos == 0 )
ListBox1 -> Items -> Add("Did Not Find");
else {
ListBox1 -> Items -> Add("Did Find");
}
}
//---------------------------------------------------------------------------



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here this works


AnsiString songs[2] = {"I Love A Cowboy", "I Love My Carhartt's");
//declare those global
//now inside your function put
AnsiString search = Edit1 -> Text;
int x;
for(x = 0, x < 2, x++){
if(StrPos(songs[x].c_str(), search.c_str())){
ShowMessage("found");
}
else {
ShowMessage("not found");
}
}
//notice the .c_str after the variables, that makes it so
//that the function StrPos can use them.

Share this post


Link to post
Share on other sites

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this