How to extract a letter out of a string?

Non-platform specific questions.

How to extract a letter out of a string?

Postby regis » Wed May 03, 2006 12:53 pm

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
Many thanks,
Regis
regis
 
Posts: 48
Joined: Tue Nov 09, 2004 10:51 am
Location: France
Score: 1 Give a positive score

Postby makslane » Wed May 03, 2006 1:24 pm

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];
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby regis » Wed May 03, 2006 1:35 pm

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...
Many thanks,
Regis
regis
 
Posts: 48
Joined: Tue Nov 09, 2004 10:51 am
Location: France
Score: 1 Give a positive score

Postby makslane » Wed May 03, 2006 2:00 pm

Variable must be declared at begin of script or begin of {
Can you show the whole code?
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby regis » Fri May 05, 2006 9:26 am

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
Many thanks,
Regis
regis
 
Posts: 48
Joined: Tue Nov 09, 2004 10:51 am
Location: France
Score: 1 Give a positive score

Postby makslane » Fri May 05, 2006 11:46 am

Variables created with variables button, are declared before all other codes, so, you don't need worry.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest