Page 1 of 1

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

Posted: 22 Sep 2011, 23:28
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.

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

Posted: 23 Sep 2011, 01:14
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()...

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

Posted: 23 Sep 2011, 08:59
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;

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

Posted: 23 Sep 2011, 13:34
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

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

Posted: 23 Sep 2011, 18:39
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 {

}