Page 1 of 1

Searching and Comparing Strings

PostPosted: Sat Oct 08, 2005 4:06 pm
by MiG3
Hi again...

I have here some code I tried to translate from another language to this one. It has errors, I don't know what they are or why. I'm hoping you could not only fix the errors, but tell me why they were errors. In detail.

This should take what the player types in, compare it to an array containing other strings, and show the strings that contain the player's string at the beginning. It would show these in the Actor's text.

Keyboard_string is a string added to everytime the player types a key.
Verbs is the array of possible strings.

[i]
i=0;
j=0;

if (keyboard_string!="")
{

while (verbs[i]!="")
{
char * strncpy(char *temp_string, const char *verbs[i], size_t strlen(const char *keyboard_string));

if (char *strncmp(char *keyboard_string, const char *temp_string, size_t strlen(const char *keyboard_string))==1)

{
if (j<=6)
{
strcpy(text,verbs[i]);
strcpy(text,"#"); // I don't know how to put a return symbol, this is what I used before.
j+=1;
}
}
i+=1;
}
}



MiG3

PostPosted: Sun Oct 09, 2005 10:00 pm
by makslane
You can use the C strstr function to search strings, like this:

if(strstr("your text", keyboard_string) != 0)
{
//text found
}

There is no strstr function on Game Editor, but you can put the code in the Global code and use:

Code: Select all
char *strstr(char *string, char *substring)
{
    char *a, *b;
   
    b = substring;

    if (*b == 0) return string;
   
    for ( ; *string != 0; string += 1)
    {
      if (*string != *b) continue;
      
      a = string;
      while (1)
      {
         if (*b == 0) return string;
         if (*a++ != *b++) break;
      }

      b = substring;
    }

    return NULL;
}

PostPosted: Mon Oct 10, 2005 2:14 pm
by MiG3
Okay, this is my first use of the global code editor but I think I might be getting it. Here's my current problem:

In the global code editor I have your code (I assume there were no changes neccesary):



char *strstr(char *string, char *substring)
{
char *a, *b;

b = substring;

if (*b == 0) return string;

for ( ; *string != 0; string += 1)
{
if (*string != *b) continue;

a = string;
while (1)
{
if (*b == 0) return string;
if (*a++ != *b++) break;
}

b = substring;
}

return NULL;
}


In the draw_event I have the function that calls it:


i=0;
while (verbs[i]!="");
{
int aa=strstr(verbs[i],keyboard_string);

if (aa=="")
{
strcpy(text,verbs[i]);
strcpy(text,"/");
}

i+=1;}


Here is my verb array:

verbs[0]="weld";
verbs[1]="wield";
verbs[2]=""; //signals end of verbs

Anyway. Problem is the game freezes. I'm not sure why. You can still hit escape to exit, though. I'm guessing that's not a step-by-step event. But why doesn't this code work?

Thanks,
MiG3

PostPosted: Mon Oct 10, 2005 3:16 pm
by makslane
To copare strings you must use the strcmp function:

if(strcmp(verbs[i], "") == 0)
{
//Same strings
}

PostPosted: Mon Oct 10, 2005 7:07 pm
by MiG3
Still not working correctly, so:

Is this correct? It's the draw event code.


i=0;

if (strcmp(keyboard_string,"")==0)
{


while (strcmp(verbs[i], "") == 0 );
{
int aa=strstr(verbs[i],keyboard_string);

if (strcmp(aa,"")==0 )
{
strcpy(text,verbs[i]);

}

i+=1;}

}


and is this, the global code, correct?

char *strstr(char *string, char *substring)
{
char *a, *b;

b = substring;

if (*b == 0) return string;

for ( ; *string != 0; string += 1)
{
if (*string != *b) continue;

a = string;
while (1)
{
if (*b == 0) return string;
if (*a++ != *b++) break;
}

b = substring;
}

return NULL;
}


Dratted C. How I hate it. Thanks for helping though.

MiG3

PostPosted: Mon Oct 10, 2005 8:21 pm
by makslane
The strstr code is ok, but I need know the other variables.
Can you send me the ged file?

PostPosted: Tue Oct 11, 2005 9:59 pm
by MiG3
I sent the ged.

MiG3

PostPosted: Wed Oct 12, 2005 1:10 pm
by makslane
What's the subject?

PostPosted: Wed Oct 12, 2005 1:50 pm
by MiG3
It's "Here's the Gmd" :P Sorry I'm used to Game Maker's files. Btw, what does the "d" stand for in gmd and ged?

MiG3

PostPosted: Wed Oct 12, 2005 2:23 pm
by makslane
Sorry, I have deleted your email...

If you need execute the code when keyboard_string is not empty use:

if (strcmp(keyboard_string,"") !=0 )
{
...
}

Or

if (strlen(keyboard_string) > 0 ) //test the lenght of string
{
...
}

PostPosted: Thu Oct 13, 2005 3:31 am
by Joshua Worth
MiG3 wrote:Btw, what does the "d" stand for in gmd and ged?
The "d" is following the "e" for "ed" as in "editor"

PostPosted: Thu Oct 13, 2005 7:16 pm
by willg101
Oh..
I though GED stood for Game Editor Data.
lol oops! :oops:

PostPosted: Fri Oct 14, 2005 12:08 am
by Joshua Worth
willg101 wrote:Oh..
I though GED stood for Game Editor Data.
lol oops! :oops:
Oh, sorry, maby it does. Try asking makslane.