NXC: string-upcase? string-downcase?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: string-upcase? string-downcase?

Post 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)
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: NXC: string-upcase? string-downcase?

Post 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);
}
Thanks, and have a nice day,
nxtboy III

programnxt.com
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: string-upcase? string-downcase?

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: string-upcase? string-downcase?

Post 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!
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: string-upcase? string-downcase?

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: string-upcase? string-downcase?

Post 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)

;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: string-upcase? string-downcase?

Post by afanofosc »

See this post:

https://sourceforge.net/apps/phpbb/mind ... 150#p12086
uplow.nxc
(393 Bytes) Downloaded 293 times
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: string-upcase? string-downcase?

Post by HaWe »

sry, what is this link for?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: string-upcase? string-downcase?

Post by mightor »

Doc,

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)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: string-upcase? string-downcase?

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests