943,649 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8581
  • C++ RSS
Sep 1st, 2009
0

How to check if string only contains (A-Z 0-9)

Expand Post »
I am doing a fuction where I want to check that a String contains only Letters between: "A-Z" and Numbers between: "0-9" and "_"

Instead of checking all one by one. Is there any approach to check against all at the same time ?

C++ Syntax (Toggle Plain Text)
  1. String ThisString= TextBox1.Text.Trim().ToLower();
  2. for (int i = 0; i < ThisString.Length; i++)
  3. {
  4. if( ThisString.Substring(i, 1) == ??? )
  5. {
  6. //Found a valid character so go on with code
  7. }
  8. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 1st, 2009
0

Re: How to check if string only contains (A-Z 0-9)

Click to Expand / Collapse  Quote originally posted by Jennifer84 ...
I am doing a fuction where I want to check that a String contains only Letters between: "A-Z" and Numbers between: "0-9" and "_"

Instead of checking all one by one. Is there any approach to check against all at the same time ?

C++ Syntax (Toggle Plain Text)
  1. String ThisString= TextBox1.Text.Trim().ToLower();
  2. for (int i = 0; i < ThisString.Length; i++)
  3. {
  4. if( ThisString.Substring(i, 1) == ??? )
  5. {
  6. //Found a valid character so go on with code
  7. }
  8. }
As far as I know you cannot check an entire string at once. You have to do it a character at a time. I could be wrong, but I don't think so.

By "checking all" and "checking one by one", if you mean check to see if a character is an 'A', then check to see if it is a 'B', then a 'C', then a 'D', etc., you definitely don't need to do that.

cctype comes in handy here.

http://www.cplusplus.com/reference/clibrary/cctype/

Everything is done by single characters, but it has functions like isdigit, isalnum, isalpha, isupper that can help here, so you can check whether a character is a digit with isdigit and whether it's an upper case letter using isupper, then check for '_'. So that's three checks per character.

Can't think of any way to do it faster.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 1st, 2009
0

Re: How to check if string only contains (A-Z 0-9)

That sounds nice. I will take a look at your suggestions and see how that could be done.

This was checking character by character:
C++ Syntax (Toggle Plain Text)
  1. String AllChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
  2. String ThisString = TextBox1.Text.Trim().ToLower();
  3. int NoValid = 0;
  4. int PartValid = 0;
  5. for (int u = 0; u < ThisString.Length; u++)
  6. {
  7. PartValid = 0; //Reset
  8. for (int q = 0; q < AllChars.Length; q++)
  9. {
  10. if (ThisString.Substring(u, 1) == AllChars.Substring(q, 1))
  11. {
  12. PartValid = 1;
  13. }
  14. }
  15. if (PartValid == 0)//Found an invalid character;
  16. {
  17. NoValid = 1;
  18. break;
  19. }
  20. }
  21.  
  22. if (NoValid == 1)
  23. {
  24. //Found invalid character
  25. }
  26.  
  27. if (NoValid == 0)
  28. {
  29. //Valid
  30. }
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 1st, 2009
0

Re: How to check if string only contains (A-Z 0-9)

Standard C and C++ have isalnum .

[edit]Sometimes the is* functions are implemented as lookup tables. Consider that type of approach (lookup table) yourself instead of the inner loop if speed is paramount.

[edit=2]Geez I need a nap:
cctype comes in handy here.

http://www.cplusplus.com/reference/clibrary/cctype/

Everything is done by single characters, but it has functions like isdigit, isalnum, isalpha, isupper that can help here, so you can check whether a character is a digit with isdigit and whether it's an upper case letter using isupper, then check for '_'. So that's three checks per character.
Last edited by Dave Sinkula; Sep 1st, 2009 at 5:54 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Massive overhauld needed on fundamentals
Next Thread in C++ Forum Timeline: Square root in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC