Solved

Checking for a Character in a String of Numbers

Posted on 1998-02-08
6
222 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:Jose_G
6 Comments
 
LVL 2

Accepted Solution

by:
Ready4Dis earned 100 total points
ID: 1181145
Here is the source.

#include <conio.h>
#include <stdio.h>

#define LENT 10
// LENT = length of string.

main ()
{
  int ctr, character;
  char array[10];
  array[0]='0';
  array[1]='1';
  array[2]='R';
  for (ctr=0;ctr<LENT;ctr++)
    if (array[ctr]<'0'||array[ctr]>'9') character=1;
}
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1181146
Jose_G
When you say string, are you talking about a character array,
or do you mean a String object.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1181147
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;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Jose_G
ID: 1181148
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.
0
 

Author Comment

by:Jose_G
ID: 1181149
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.
0
 

Author Comment

by:Jose_G
ID: 1181150
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now