Page 1 of 1

Working with strings

PostPosted: Tue Jan 10, 2012 9:10 am
by bsi
I have some problems working with strings. I have read the manual and I have seen that Game-Editor provides some functions to be used with strings. However, I cannot understand how to perform certain operations. In particular:

1) How to convert an integer into a string. Game-Editor provides the function for string-to-integer (atoi) but not the reverse (or at least I have not found it).

2) I have two variables declared string st1 and st2. I want to assign st2 a substring of st1 starting at position m and lenght n. From what I read substrings of controlled lenght can be copied into another string but only starting at the beginning (strncpy).

Thanks in advance.

Re: Working with strings

PostPosted: Tue Jan 10, 2012 6:37 pm
by skydereign
You can use sprintf to convert most variable types into a string. In your case you want %d for the format. The following will convert integer into the string.
Code: Select all
sprintf(string, "%d", integer);

You will have to use strncpy, but since a string is a character array, you can choose the starting point of the string.
Code: Select all
strncpy(str2, &str1[5], 5);

That will copy 5 characters starting from the 6th character.