a-z, 1-9 in c++?

ok so i forgot, but in php or perl, or maybe both, i could compare lets say a string with the letters a to z or A to Z by doing a-z, A-Z respectively instead of having to type out all the letters themselves. the game goes with numbers.

my question is how do i do this in c++? cause i've tried to the it with the same syntax and it didn't work
closed account (Lv0f92yv)
Are you talking about regular expressions? IE: [A-Za-z0-9] - a regex that mathes any character a through z inclusive, and digits?

If this is the case, try: http://msdn.microsoft.com/en-us/library/a9z6549f%28v=VS.80%29.aspx
That link is about C++/CLI, not C++
For C++ regex check boost: http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/index.html
Something like that will be included in C++0x
closed account (Lv0f92yv)
Sorry for the bad link, thanks for the correction.
are these libraries standard libraries for c++? or do i have to download them? cause i can only use the default libraries
Boost are libraries for C++. They are not part of the current standard library but they are planned to be included in the next standard.
No... they are anything BUT standard. But you should be using Boost anyway.

EDIT: Bazzy beat me.

EDIT2: However, if you want to ensure that what you're fetching are alphanumeric characters...
http://cplusplus.com/reference/clibrary/cctype/

-Albatross
Last edited on
the reason i can't use it is because im doing a school project and i can only use standard libraries.

so there is no other ways to do reg expressions on c++ with external libs?
@albatross, thanks for the link
Forget the regex stuff; it is probably WAY overkill for what you need. Follow Albatross' link to cctype and
use isalpha(), isdigit(), isalnum(), etc.
you're right, it is overkill but i thought that it was built in c++ like it is in perl.

i have a question, those functions in cctype only works with individual characters. im using strings. but when it did for ex, isdigit(string[1]), it did not work. i mean isn't a string a char array? so shouldn't string[1] actually be a char? i don't want to convert every string to chars, especially since they should already be a char array and also im dealing with a very large problem and many words.
You could also just check the ASCII value of the characters. For example:
1
2
3
4
bool isDigit( char c )
{
    return c >= '0' && c <= '9';
}
Last edited on
@moorecm
That's a bit longer, but would do about the same thing. Still, it is longer...

@jinjin12
Check the return type of the [] operator. It returns char&, but isDigit takes a char or int.

-Albatross


EDIT: i386 posts, and counting.

...subtract 80,000 from that.
Last edited on
@ Albatross ahhh i see, thanks alot man, i guess i just have more work to do then. but for future references , how do i check the return type of stuff, such as operators? i can check the size in bytes of something using sizeof() but how do i check for return type? is their a function or did you just your the reference on this site?
http://cplusplus.com/reference/

EDIT: Yep, I used the reference. Awesome, isn't it?

-Albatross
Last edited on
Topic archived. No new replies allowed.