• ### Popular Now

• 12
• 10
• 10
• 13
• 10

#### Archived

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

# Finding a single word within a string

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

## 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 on other sites
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 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 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 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

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.

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

##### 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 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 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 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;}