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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
bool
HasNonDig(const char *StrPtr)
{
while (char CurChr = *StrPtr++)
if (CurChr < '0' || '9' < Curchr)
return true;
return false;
}
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.
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.
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.
When you say string, are you talking about a character array,
or do you mean a String object.