Scanning A String For A Match

Posted by Daniel Spain on Sun 13 Nov 2011 10:07 AM — 2 posts, 14,523 views.

#0
in regards to games i am trying to find the best way to search a string for a shorthand match on a target.

example:

say you are in a room fighting a 'black dragon'
i want you to be able to use any characters from left to right
like:

attack b
attack bla
attack dra
attack dragon

any of those i want to work.

here is what i have been using on other programs:


bool sameto(const char *shorts,const char *longs)
/* compare short string to long string */
{
	 while(*shorts != '\0')
	{
		if(tolower(*shorts) != tolower(*longs)) 
		{
			return(false);
        }
        shorts++;
        longs++;
    }
    return(true);
}


this works fine as long as there are no spaces in the name.

so if the monster is "dragon" any character match from the d to the n it will take, ie: "attack dra"
however when it gets trickier like "black dragon"
anything within the name black it will take but the space stops it from matching because of the


while(*shorts != '\0')


any ideas?
USA #1
A space isn't '\0', it's '\40'. So that's not your problem.

The problem is, you want to be able to match from the start of any word in the name, but you're only checking from the start of the name. One thing you might want to try is splitting the name into words, and checking the input against each word. There might be more efficient methods that I haven't considered, but that's pretty much the most straightforward one.