[NXC] Can I querey a string to determine it's case?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
dodgey
Posts: 19
Joined: 17 Sep 2011, 23:27

[NXC] Can I querey a string to determine it's case?

Post by dodgey »

Hi - I want to query a text string and see if the result is either..

Upper case
Lower case
Numeric

Is this possible? (irrelevent of the content, I just want to know case or numeric)

Otherwise I've got to query the string value and "say" if it's any one character of "DURLBF" or "123456" or "durlbf" then do something.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] Can I querey a string to determine it's case?

Post by muntoo »

Code: Select all

enum szcase
{ none = 0, upper, lower, both, num };

int n;
string sz = "this is a string";
szcase sc = none;

if(sscanf(sz, "%d", n) == EOF)
{
    for(unsigned int i = 0; i < strlen(sz); ++i)
    {
        if(sz[i] >= 'A' && sz[i] <= 'Z')
            sc |= upper;

        if(sz[i] >= 'a' && sz[i] <= 'z')
            sc |= lower;
    }
}
else
{
    sc = num;
    NumOut(0, 0, num, 0);
}
I'm not sure if NXC supports sscanf()...
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
dodgey
Posts: 19
Joined: 17 Sep 2011, 23:27

Re: [NXC] Can I querey a string to determine it's case?

Post by dodgey »

Wonderful! Many thanks. I'll have a play.

Even if scanf isn't supported I didn't realise you could "do" <>A-Z <>a-z etc,

... in fact, my string will only ever contain one character/numeral, so I can just use. coolio!

{
if(sz >= 'A' && sz <= 'Z')
sc |= upper;

if(sz >= 'a' && sz <= 'z')
sc |= lower;
}
}
else
{
sc = num;
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Can I querey a string to determine it's case?

Post by afanofosc »

Read the character one time from the string like this:

Code: Select all

char ch = sz[0]; // the one and only character in the string
Then, if you want, you can use the standard library ctype routines for checking characters.

http://bricxcc.sourceforge.net/nbc/nxcd ... ample.html

e.g., isupper, islower, isdigit

You don't need the OR-equal stuff (|=) since you are only looking at a single character.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] Can I querey a string to determine it's case?

Post by muntoo »

Oh, right, forgot about those nifty little is_()s.

Code: Select all

char ch = sz[0];

if(isdigit(ch)) {

}
else if(isupper(ch)) {

}
else if(islower(ch)) {

}
else {

}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests