Working with strings

Non-platform specific questions.

Working with strings

Postby bsi » Tue Jan 10, 2012 9:10 am

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.
bsi
 
Posts: 6
Joined: Thu Jan 05, 2012 9:24 am
Score: 3 Give a positive score

Re: Working with strings

Postby skydereign » Tue Jan 10, 2012 6:37 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest