Page 1 of 1

How to extract a letter out of a string?

PostPosted: Wed May 03, 2006 12:53 pm
by regis
I have a string variable: let's say: NAME.
For instance, suppose NAME equals "andrew smith"
A would like to get the 1st letter of NAME (here 'a') and put it in the variable LETTER1 (also declared as a string in GE)
Unfortunately, the following code does not seem to work:

char LETTER1 = NAME[0];

This lead to the following error message: "Incompatible types: cannot convert from 'char' to 'ARY[256]char'.
Why?
Many thanks for your help.
Regis

PostPosted: Wed May 03, 2006 1:24 pm
by makslane
If the string is a actor variable, like name, you can't access character directly.
So, you need to copy first to other string, and get the character from this string:

char s[32]; //the temp string
char LETTER1;

strcpy(s, name);
LETTER1 = s[0];

PostPosted: Wed May 03, 2006 1:35 pm
by regis
Hi Makslane,
Once again, I am impressed by the rapidity of your answer! :D
The problem is:
Entering the following code you give:
char s[32];
... Leads to another error message: "Unexpected declaration". :?:
I am sorry, because this may be due to my lack of knowledge.
By the way, the variable NAME is in reality the result of the following code:
strcpy(NAME,getOwner());
May help...

PostPosted: Wed May 03, 2006 2:00 pm
by makslane
Variable must be declared at begin of script or begin of {
Can you show the whole code?

PostPosted: Fri May 05, 2006 9:26 am
by regis
Yes, Makslane: you are perfectly right (as usual :wink: ): arrays must be declared at the beginning of the code: is it because such a way of declaring variables is different from using the GE menu "variable" and such a menu declares variables at the beginning too (while compiling) but this is "transparent" for the user?
Your friend,
Regis

PostPosted: Fri May 05, 2006 11:46 am
by makslane
Variables created with variables button, are declared before all other codes, so, you don't need worry.