Finding a single word within a string

Started by
17 comments, last by phil05 20 years, 1 month ago
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.
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
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)<br>            location = -1;<br>        location = x;<br>    }<br>  <br>    if(location >= 0)<br>        return location;<br>} </pre> <br>That's the algorithm.  Whether or not there's an ANSI C function that does it, I'm not sure. <br><br>Later,<br>ZE.   <br><br><SPAN CLASS=editedby>[edited by - zealouselixir on March 21, 2004 10:30:20 PM]</SPAN>

[twitter]warrenm[/twitter]

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.
enum Bool { True, False, FileNotFound };
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
Things change.
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.
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;} 
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.
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry
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;} 
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.

This topic is closed to new replies.

Advertisement