Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Scanning A String For A Match

Scanning A String For A Match

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Daniel Spain   (15 posts)  Bio
Date Sun 13 Nov 2011 10:07 AM (UTC)
Message
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?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Sun 13 Nov 2011 06:02 PM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,781 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.