strings

Posted By: Amanda_Dearheart

strings - 06/15/09 15:10

If I create a string such as

String *MyString = "Hi out there"

is there a way I can retrieve individual characters such as
the letter H, and then the letter i, and then the letter o, etc. etc. etc.

Also does the string support concatenation, such as

Mystring = String1 + String2

thanks for any help!
Posted By: MMike

Re: strings - 06/15/09 15:18

It does support concatenation, but not that way, as i know.
For that you use the Gs function: str_cat( string to pointer, string from pointer)
the string from pointer can be like this.. str_cat(str1,"second text added");

to retrieve individual chars, you can use str_trunc and clip, manipulations, though i think there might be a high chance ( i didnt explore yet) about char[char-number] options.
Posted By: Ottawa

Re: strings - 06/15/09 23:25

Hi!

I used str_clip at one point.

I transfered all the letters of the string to numbers
put the numbers in an array

and then I used: if the number is ....


Ottawa smile
Posted By: Gordon

Re: strings - 06/16/09 00:20

or you could use this little function cool

Code:

#include <acknex.h>

STRING* str1="This is a Test";

char GetChar(STRING* str, int chr)
{
	return((str.chars)[chr]);
}


void main()
{
	wait(1);
	char c;
	
	c = GetChar(str1,0);
	if (c == 'T') {	
		error("passed");
	} else {
		error("fail");
	}
}

Posted By: CharlieAmer

Re: strings - 06/16/09 21:01

you may treat a string as a array of chars with length (size) of strlen() .
so, if you have a something like this:

STRING* strng="HELLO";

than function strlen(strng) will return 5.
to access first letter u use strng[0], second letter strng[1], .. etc, fifth letter strng[4];
so don't access a string like array from 1 to strlen(). access it like array from 0 to strlen()-1;
Posted By: Gordon

Re: strings - 06/17/09 13:24

Quote:
to access first letter u use strng[0], second letter strng[1]


While this will work in C for a char array it will NOT work in lite-c.

STRING is defined in atypes.h as
Code:
typedef struct STRING {
	C_LINK	link;
	char	*chars;		// pointer to null terminated string
	long	length;		// allocated length of string
	long	flags;		// see STRF_... above
    byte    pad[PAD_STRING];
} STRING;


Therefore you must access the character data as (strname.chars)[index]. The first character is index = 0 etc. Just remember that STRING* is not the same as char*.
© 2024 lite-C Forums