Solved

Checking for a Character in a String of Numbers

Posted on 1998-02-08
6
218 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

18 Experts available now in Live!

Get 1:1 Help Now