Solved

Checking for a Character in a String of Numbers

Posted on 1998-02-08
6
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

617 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