Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking for a Character in a String of Numbers

Posted on 1998-02-08
6
Medium Priority
?
251 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses

722 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