Page 1 of 2
NXC: string-upcase? string-downcase?
Posted: 02 Feb 2012, 20:15
by HaWe
hi,
do we already have functions for string upcase/downcase in NXC?
I know that there's toupper but it's designed for a single char, not for strings (CMIIW)
Re: NXC: string-upcase? string-downcase?
Posted: 02 Feb 2012, 23:19
by nxtboyiii
just do this:
Code: Select all
task main()
{
string s="lorem ipsum";
char s_data[];
byte length=StrLen(s);
ArrayInit(s_data,0,length);
StrToByteArray(s,s_data);
for(int c=0; c < ArrayLen(s_data); c++)
{
toupper(s_data[c]);
}
ByteArrayToStr(s_data);
}
Re: NXC: string-upcase? string-downcase?
Posted: 03 Feb 2012, 00:32
by afanofosc
I don't think that will work.
Try this:
Code: Select all
task main()
{
string s="lorem ipsum";
TextOut(0, LCD_LINE1, s);
for(int c=0; c < strlen(s); c++)
s[c] = toupper(s[c]);
TextOut(0, LCD_LINE2, s);
Wait(SEC_5);
}
John Hansen
Re: NXC: string-upcase? string-downcase?
Posted: 03 Feb 2012, 09:58
by HaWe
yes, this
for(int c=0; c < strlen(s); ++c) s = toupper(s[c]);
is just what I am already doing!
thank you!
Re: NXC: string-upcase? string-downcase?
Posted: 03 Feb 2012, 19:55
by spillerrec
If tolower and toupper were native firmware operations you could have used them on arrays too, but it is not the case. Well, there isn't a function to do this in C either AFAIK...
Any particular reason you are looking for such functions? Do you need to do this operation as fast as possible, or just curious if there was a better way to do it?
This version is a bit more efficient:
Code: Select all
for( int i=strlen(s); i>=0; i-- )
s[i] = toupper(s[i]);
Instead of checking the array length each time, it will only be done once in this hence the performance boost. (33%, 22 msec compared to 33 msec with "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor ".)
If you really want performance try this:
Code: Select all
{
int i = strlen( s );
unsigned char c;
loop_start:
asm{
brtst LTEQ, loop_end, i
sub i, i, 1
index c, s, i
brcmp LT, loop_start, c, 97
brcmp GT, loop_start, c, 122
sub c, c, 32
replace s, s, i, c
jmp loop_start
loop_end:
}
}
This is ~66% (9 msec) compared to the original and even faster if the string contains a lot of non-lower case character as it only overwrites the character if it indeed is lower case.
Re: NXC: string-upcase? string-downcase?
Posted: 03 Feb 2012, 21:01
by HaWe
no, thanks, I'm programming in NXC, never in NBC
(speed is no issue, and if it was, I would wish the fw would work faster)
;)
Re: NXC: string-upcase? string-downcase?
Posted: 06 Feb 2012, 00:34
by afanofosc
Re: NXC: string-upcase? string-downcase?
Posted: 06 Feb 2012, 07:58
by HaWe
sry, what is this link for?
Re: NXC: string-upcase? string-downcase?
Posted: 06 Feb 2012, 08:47
by mightor
Doc,
Did you read the post John linked?
- Xander
Re: NXC: string-upcase? string-downcase?
Posted: 06 Feb 2012, 10:52
by HaWe
yes of course but I didn't understand what this is for (asm, chunkID,...?)
does it mean there now are new functions in the API called UpperCase and LowerCase for strings?