• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

checking for characters

I have a string that I need to analyze.
How would I be able to tell if there are any characters in the string?  I basically need to make sure there are only numbers in the string and no alpha characters.
ie
121342  is good
124a32  is bad

Thanks for your help.
0
andrew161
Asked:
andrew161
  • 3
1 Solution
 
andrew161Author Commented:
Adjusted points to 150
0
 
nietodCommented:
you can use the isalpha() function contained in <ctype.h> to test each character

example follows.
0
 
nietodCommented:
bool IsAlphaString(const string &S)
{
   const int Len = S.length();

   for (int i = 0; i < Len; ++i)
        if (isalpha(S[i]))
           return true;
   return false;
}
0
 
nietodCommented:
Actually, that only makes sure that there are no letters.  That doesn't test for spaces and symbols. If you only want digts you need to use isdigits(), like

bool isDigitString(const string &S)
{
   const int Len = S.length();

   for (int i = 0; i < Len; ++i)
      if (!isdigit(S[i]))
         return false;
   return true;
}

let me know if you have any questions.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now