Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, Grant, Neb), 908 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
get a single character from string #261243
04/16/09 14:14
04/16/09 14:14
Joined: Mar 2006
Posts: 2,252
Hummel Offline OP
Expert
Hummel  Offline OP
Expert

Joined: Mar 2006
Posts: 2,252
I wondered why there is still no Lite-C function wich returns a single character from a defined position of a string, like this:

STRING* str1="";
STRING* str2="TestString";

str_get_char(str1,str2,5);//str1 now contains "S"

Re: get a single character from string [Re: Hummel] #261247
04/16/09 14:33
04/16/09 14:33
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
some time ago (i don't know where) jcl said that a direct access to the "chars" is not "allowed" or "wished". But I'm pretty sure that with the given cautiousness the following code should be safe:

Code:
char* letter5 = str->chars[4]; //direct cast to a char pointer
char letter[2] = str->chars[4]; letter[1] = 0; //cast to a char array

//string function
function str_get_char(STRING* str1,STRING* str2,var m_val)
{
  str_cpy(str1,"");
  str_to_asc(str1,str2->chars[m_val]);
}


Re: get a single character from string [Re: Hummel] #261249
04/16/09 14:38
04/16/09 14:38
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
no, but you could do this

str_cpy(str1, str2);
str_clip(str1, 5);
(str1.chars)[1] = 0;

should do it.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: get a single character from string [Re: EvilSOB] #261268
04/16/09 15:59
04/16/09 15:59
Joined: Mar 2006
Posts: 2,252
Hummel Offline OP
Expert
Hummel  Offline OP
Expert

Joined: Mar 2006
Posts: 2,252
ok,it works - thx!

void str_get_chars(STRING* str1,STRING* str2,int start_num,int count)
{
str_cpy(str1, str2);
str_clip(str1, start_num-1);
(str1.chars)[count] = 0;
}

str_get_chars(str,"A new function for Lite-C",7,8);//str contains "function"

have a nice day! laugh

Re: get a single character from string [Re: Hummel] #261283
04/16/09 16:40
04/16/09 16:40
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
very good idea laugh (do not react on sarcasmn wink )

Okay but i also want to explain..
Imagine the following: You have a string with the length of 10.000 characters. You want to get the second character.

1) str_cpy is SLOW in this case
2) you set the 3 character to zero in this case, and by that signalize that the string ends here. ==> 9.997 characters of endless memory waste smile at least take str_trunc instead of setting a char to zero.

Re: get a single character from string [Re: TechMuc] #261334
04/16/09 22:51
04/16/09 22:51
Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
VeT Offline
Serious User
VeT  Offline
Serious User

Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
Code:
void str_get_chars(STRING* str1,STRING* str2,int start_num,int count) 
{
str_cpy(str1, str2);
str_clip(str1, start_num-1);
(str1.chars)[count] = 0;
}

str_get_chars(str,"A new function for Lite-C",7,8);//str contains "function"

very usefull snippet, btw smile
well, i may write it somewhere, not to lose


1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy

Newton2 videos: http://tinyurl.com/NewtonVideos
LiteC+Newton2 discussion: http://tinyurl.com/NewtonWrapperDiscussion
Latest LiteC+Newton2 version(v23, from 29.10.2009): http://depositfiles.com/files/ae1l0tpro
Re: get a single character from string [Re: VeT] #261358
04/17/09 07:07
04/17/09 07:07
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
A variation of Hummels variation of my code - for TechMuc
(slower in number of instructions, but more efficient in memory used.

Code:
//(this is untested)
void str_get_chars(STRING* dest,STRING* source,int start_num,int count) 
{  
   str_cpy(dest, "");
   int c; for(c=0; c<count; c++)
   {
       str_cat(dest, " ");
       (dest.chars)[c] = (source.chars)[start_num + c];
   }
}
This more your flavour TechMuc?


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: get a single character from string [Re: EvilSOB] #261388
04/17/09 11:38
04/17/09 11:38
Joined: Apr 2009
Posts: 33
Germany
B
Bunsen Offline
Newbie
Bunsen  Offline
Newbie
B

Joined: Apr 2009
Posts: 33
Germany
Another version (assuming start_num counts from 1):


Code:
function str_get_chars(STRING* dest, STRING* source, int start_num, int count)
{
	str_cpy(dest, ""); // Clear destination STRING object. 
	int srcLen = str_len(source); // Get len of source string.
	
	if (start_num > 0 && count > 0 && srcLen > start_num) // Check ranges.
	{
		char *szTmp = malloc(count + 1); // Create temp buffer.
		
		int i = 0;
		while (i < count && i + start_num - 1 < srcLen) // Count while in range.
		{
			szTmp[i] = (source.chars)[i + start_num - 1]; // Copy chars.
			i++;
		}
		szTmp[i] = 0;	
		str_cat(dest, _str(szTmp)); // Store final result into dest STRING object. 	
		free(szTmp);
	}
}


Re: get a single character from string [Re: Bunsen] #261408
04/17/09 13:53
04/17/09 13:53
Joined: Mar 2006
Posts: 2,252
Hummel Offline OP
Expert
Hummel  Offline OP
Expert

Joined: Mar 2006
Posts: 2,252
...you could also use " str_trunc(str1,str_len(str1)-count);" instead of "(str1.chars)[count] = 0;" in my version of the function...


Moderated by  aztec, Spirit 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1