Link to home
Start Free TrialLog in
Avatar of Jose_G
Jose_G

asked on

Checking for a Character in a String of Numbers

Is there a function (custom made or included) that will test if a non-digit character is in a string?  For example, if a char string has a value of 37r9, what function can I use to test if an intended string of digits contains a non-digit character?
ASKER CERTIFIED SOLUTION
Avatar of Ready4Dis
Ready4Dis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of q2guo
q2guo

Jose_G
When you say string, are you talking about a character array,
or do you mean a String object.
ready4dis's code will work, but is not the most efficient.  If you string is NUL termianted you probably won't have the length so a for loop is out.  There is no need to test every character, that is, once a nojn-digit is found you can stop. Also for efficiency array subscripting should be avoided, especially in loops.  Finally you will want this in a function form.  I would recomend

bool
HasNonDig(const char *StrPtr)
{
  while (char CurChr = *StrPtr++)
     if (CurChr < '0' || '9' < Curchr)
        return true;
  return false;
}
Avatar of Jose_G

ASKER

Thanks nietod.  I had to modify your code a little to work for me, but it gave me an idea of what I needed to do.
Avatar of Jose_G

ASKER

Thanks nietod.  I had to modify your code a little to work for me, but it gave me an idea of what I needed to do.
Avatar of Jose_G

ASKER

Thanks nietod.  I had to modify your code a little to work for me, but it gave me an idea of what I needed to do.