Advertisement

How do I find a word in a string?

Started by June 04, 2001 12:48 PM
4 comments, last by griffenjam 23 years, 8 months ago
I know this probably really easy and I should really know how to do this, but in C++ how do see if a word is in a string (or array of strings)? For example. I have a list of strings that contain subject headings, and the user types in "water", I want to find all subjects that contain "water". All I can think of is checking for the fist letter in each string untill it''s found, then checking for the second and so on. But in a list of 1000+ string this seems like this would take a long time, as I am looking for a realtime response, You would want to talk to a NPC and have him say "Let me prepare my answer, come back later" everytime you asked something. Jason Mickela ICQ : 873518 E-Mail: jmickela@pacbell.net ------------------------------ "Evil attacks from all sides but the greatest evil attacks from within." Me ------------------------------
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Unless you index the important words in a string or something like that, iterating through them all is the only way to find if a word is in them(surprise: you can''t tell what''s there without looking at it). I think you overestimate the power required to iterate all their strings. If you only have to iterate through all the strings a few(or even once) don''t worry about it. If you need to go through them a helluva lot of times in one second, then indexing might be a good idea.

Hamhed
Advertisement
strstr() is a function that checks for a given string inside another string. Take a look at the documentation for actual syntax. I use this just for that purpose.
Kevin

-----------------------------
kevin@mayday-anime.com
http://dainteractive.mayday-anime.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
Hi,

You might want to try using a std::string class

#include &ltstring>

using std::string;

// declare the string
string mystring("The water is cold");

// try to find the string
string::size_type pos = mystring.find("water");
if(pos != string::npos)
{
// found - do something here
}

std::string::npos can be though of as ''not-found''. If one of the search functions returns this, you know what you searched for isn''t there.

Also remember that the STL std::string (actually std::basic_string which is typedef''d as std::string) is case-sensitive. This is due to the character traits passed to the template class basic_string. If you want case-insensitive comparisons you must make sure you make all your strings lower case OR define your own char traits class.

If you have anymore questions let me know.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
That is a good starting point, but once you have that you might as well start indexing it. I assume you decided against menus and dialog trees so that the npcs would seem more intelligent. It takes a lot of work to beat out menus and dialog trees though otherwise they wouldn''t be as common as they are. Building indexes to support full text searchs is fairly simple compared to actually getting the npc to respond in an intelligent manner. You may not need it for the speed, but the practice will serve you well when it comes to adding keywords, classifications and contexts to the strings. The starting point for indexing it is breaking the strings up into words and counting how many times each word appears. The fewer times a word appears the more selective it is in a search, i.e. "the" is not very selective.
Keys to success: Ability, ambition and opportunity.
Thanks for all the help, I knew there had to be a better way to do it!
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery

This topic is closed to new replies.

Advertisement