View unanswered posts | View active topics It is currently Mon Nov 24, 2014 10:14 pm






Reply to topic  [ 5 posts ] 
Finding the size of strings 
Author Message
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Finding the size of strings
Hey,

I'm trying to figure out the size of a string -- in other words, I want to know how many characters it contains. So the string containing "Hello", for example, should give me "5".

So this is what I tried: the code below was supposed to display how long the string was, but I never got anything other than 20 (I tried different string sizes, different variable names, etc.). Here's my code:
Code:
task main(){
  string testString = "Hello";
  int testSize = sizeof(testString);
  nxtDisplayCenteredBigTextLine(3, "%d", testSize);
  wait10Msec(500);
}
Any ideas?

Thx,

_________________
leonoverweel.com


Mon Jan 30, 2012 7:25 pm
Profile WWW
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Finding the size of strings
Hi dimastero!

I believe you want the strlen() function.


Mon Jan 30, 2012 8:13 pm
Profile
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: Finding the size of strings
Thanks! That was exactly what I was looking for :)

_________________
leonoverweel.com


Mon Jan 30, 2012 9:03 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post Re: Finding the size of strings
Internally, strings are always 20 long, this is no coincidence, it's the amount of characters you can put on the screen.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jan 31, 2012 1:26 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Finding the size of strings
mightor wrote:
Internally, strings are always 20 long, this is no coincidence, it's the amount of characters you can put on the screen.

That's one of the quirks about RobotC that always trips me. IMHO, strings can be used in a lot of situations, not just displaying on the LCD screen. For example, I could do string manipulation to/from a file. Limiting it to 20 characters seems short sighted. Another example is limiting the debug stream with the same restriction. And I can't even print a string with more than 2 substitutions.


Tue Jan 31, 2012 7:00 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.