[HELP] Strings
#1

Can Someone Explain me This Code in breif:

PHP код:
new string1[6] = "Hello";
new 
string2[6] = "Hello";
for (new 
idid<6id++)
{
    if(
string1[id] != string2[id]) return false//If the strings don't equal (They do though).
}
return 
true
Reply
#2

You've got 2 strings with the samelength
the
pawn Код:
for(new id; id<6; i++)
is starting a loop.. meaning "you take a variable called "id" and let it increase by 1 each round until it reached 6, then the loop stops (the loop will stop after id = 5)

in the loop you check
pawn Код:
if(string1[id] != string2[id])
depending by the number of "id" the letter of the string will be called:
e.g. string1[0] would be 'H', string1[3] would be 'l' and string1[4] would be 'o'.
As the id is starting at 0 and increasing by 1 each round, it will call be called 6 times:
Time 1 id = 0;
Time2 id = 1;
Time 3 id= 2;
Time 4 id = 3;
Time 5 id = 4;
Time 6 id = 5;

You are asking whether the character at the specific number of the text in string1 IS NOT the same as the one in string2. So the loop would stop and return "false" at the first letter that doesn't match

As the strings are completely the same though, it will go through all numbers and then return "true" finally.
Reply
#3

It's simple, code is making 2 new strings with len 6 so strings have 6 places in what you can store different text.
Text is stored in each place in both strings and it's same(1 - H, 2 - e, 3 - l, 4 - l, 5 - 0 and nothing in 6th. For loop is making new variable and setts it to 0 and commands it to be less than 6. Every time when "for" loop finishes what it needs to variable "id" getts bigger for 1. Line
pawn Код:
if(string1[id] != string2[id]) return false;
is checking are same places of string1 and string2 equal. If they aren't code will return false. All 6 places are same so code will not return false, it will return true becouse of this line:
pawn Код:
return true;
! I hope it helped and I'm sorry if it wasn't so breif. Tell me if you want me to explan it to you more detailed.
Reply
#4

pawn Код:
new string1[6] = "Hello";
new string2[6] = "Hello";
for (new id; id<6; id++)
{
    if(string1[id] != string2[id]) return false; //If the strings don't equal (They do though).
}
return true;

first, two arrays are being declared and
both initialized with "Hello"
or
Код:
{'H','e','l','l','o','\0'}
Both arrays have 6 elements, indexes from 0 to 5
(as we start counting from 0 in IT)

you can see clearly that it's indeed 6 elements:
Код:
{'H','e','l','l','o','\0'}
(last being the "null-char" indecating the end)

next what we're doing is a for-loop.
Out iterator variable is called "id"
it's good practice to always set the
iterator to 0 (eg new id=0) but it's okey like this too.
All variables created in pawn are 4 Bytes long and
contain 0 (in Little-Endian) once created.

the condition of that for loop is: id < 6
meaning it'll stop looping once id is not smaller than 6.
each iteration, id is being incremented by 1 (id++)

now lets discuss whats inside the body of that loop.
There's a single control statement which returns false
if it's condition equals true.
Lets check out that condition: string1[id] != string2[id]
that'll look like: string1[0] != string2[0]
for the 1st iteration.
we're basically checking if one element of string1
is the same as one of string2.
we're using the iterator id to select the elements.
it'll look like...
1st iteration:
string1[0] != string2[0]
2nd iteration:
string1[1] != string2[1]
and so on until our condition for that loop no longer equals true
that being if id is not smaller than 6.

after that loop, for some reason unknown to me, we return true.
All this must be part of a string-checking-like function, that'd make sense.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)