Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

phil05

Finding a single word within a string

This topic is 5231 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

I need a command in c++ that searches for the word "sword" within a sentence, stored in buffer[500]. For example, If buffer contains the word "sword" in its sentence, then output this.

Share this post


Link to post
Share on other sites
Advertisement
well you have an issue first off...is the buffer sorted ? if it is you can search pretty fast using a binary search function. otherwise just search the buffer using a for loop to check every entry until you find it, i however would suggest sorting the buffer if possible because binary searches are much faster then linear for loop queries

Share this post


Link to post
Share on other sites
If you put that buffer in a std::string, you'll have the find(toFind ) function available. Otherwise, it's not too hard to imagine what you'll have to do:

short location = -1;
for(x = 0 to buffer length)
{
for(i = 0 to length of search term)
{
if(!buffer[x+i] == searchterm[ i])
location = -1;
location = x;
}

if(location >= 0)
return location;
}

That's the algorithm. Whether or not there's an ANSI C function that does it, I'm not sure.

Later,
ZE.

[edited by - zealouselixir on March 21, 2004 10:30:20 PM]

Share this post


Link to post
Share on other sites
You want strstr(), which is good-old standard C library.

Of course, if you want DELIMITED words, then you want to do some fanciness with delimited streams, using std::copy() and putting each word into a list, and then using std::find() on that list instead.

Share this post


Link to post
Share on other sites
It sounds like you''re trying to find a substring within another string ("stored in buffer[500]"), you could use

char *swd;

if((swd = strstr(buffer, "sword")) != NULL)
*do something*
else
*not found*

I ripped this from a book. It''s case-sensitive, but if you need another function that isn''t case-sensitive, write it out to strupr(first_string) strupr(second_string)...which I can post here if you can''t figure that out.



"TV IS bad Meatwad...but we f***in need it"

If you''re a girl under the age of 12, and you''re high on marijuana...don''t ride your bike. -TRUTH

Share this post


Link to post
Share on other sites
It would probably also be faster if the line was also broken down into words first. You could also then speed it up by eliminating words that couldn''t possibly be it.

You might also have to be careful with certain results.

example:
player: "I''m going to kill you with my sword."
reply: "Oh, yes I have a sword. Here it is."

A language examiner is always complicated.

Share this post


Link to post
Share on other sites
Boku, I tried this but got an error..

error C2440: ''='' : cannot convert from ''char *'' to ''char [500]''
There are no conversions to array types, although there are conversions to references or pointers to arrays
Error executing cl.exe.


#include <iostream>
#include <string>

int main()
{
using std::cout;
using std::cin;
using std::endl;

char buffer[500];

cout << "Player -> ";
std::cin.getline(buffer, sizeof(buffer));

if ((buffer = strstr(buffer, "sword")) != NULL)
{
cout << "Yes." << endl;
}
else
{
cout << "No." << endl;
}

return 0;
}

Share this post


Link to post
Share on other sites
I suggest you look up either the Boyer-Moore algorithm (which I think std::string::find uses), or if space isn''t a concern, the Suffix Search algorithm. The suffix search tends to be the fastest, but it eats up a fairly large amount of space. (About 4x the amount of space the original string uses). The algorithm mentioned by ZealousElixir is basically the naive algorithm, but if run-time isn''t a priority (or your search string is relatively small), then it''s fine to use it.

Share this post


Link to post
Share on other sites
Well, this worked but it didn''t let me ask for a sword in a sentence, such as "do you have a sword."


#include <iostream>
#include <string>

int main()
{
using std::cout;
using std::cin;
using std::endl;

char buffer[500];

cout << "Player -> ";
std::cin.getline(buffer, sizeof(buffer));

if ((buffer == strstr(buffer, "sword")) != NULL)
{
cout << "Yes." << endl;
}
else
{
cout << "No." << endl;
}

return 0;
}

Share this post


Link to post
Share on other sites
Heh... it seems like there would be a simple command that finds such a substring, but nope. It always has to be 20 lines to do one little thing.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!