NXC: string-upcase? string-downcase?
NXC: string-upcase? string-downcase?
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)
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?
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);
}
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: NXC: string-upcase? string-downcase?
I don't think that will work.
Try this:
John Hansen
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);
}
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC: string-upcase? string-downcase?
yes, this
for(int c=0; c < strlen(s); ++c) s
for(int c=0; c < strlen(s); ++c) s
= toupper(s[c]);
is just what I am already doing!
thank you!
-
- Posts: 358
- Joined: 01 Oct 2010, 06:37
- Location: Denmark
- Contact:
Re: NXC: string-upcase? string-downcase?
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: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: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.
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]);
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:
}
}
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Re: NXC: string-upcase? string-downcase?
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)
;)
(speed is no issue, and if it was, I would wish the fw would work faster)
;)
Re: NXC: string-upcase? string-downcase?
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC: string-upcase? string-downcase?
sry, what is this link for?
Re: NXC: string-upcase? string-downcase?
Doc,
Did you read the post John linked?
- Xander
Did you read the post John linked?
- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
Re: NXC: string-upcase? string-downcase?
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?
does it mean there now are new functions in the API called UpperCase and LowerCase for strings?
Who is online
Users browsing this forum: No registered users and 3 guests