Solved

Checking for a Character in a String of Numbers

Posted on 1998-02-08
6
228 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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