The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Testing characters
Discuss Testing characters in the C Programming forum on Dev Shed. Testing characters C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 14th, 2012, 01:36 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
|
Testing characters
I'm trying to understand how to write a program where I have to have a function testing the characters of input. I understand some basics, such as a validity check ! or EOF, and detecting digits. However, it gets complicated when I want to check for digits and a decimal, and making sure only a set amount of digits are past the decimal.
For starters, I'm a bit of a doorknob with understanding C. It took me almost a month to understand the simple concept of functions and how to write multiple functions. Developing a function to test for acceptable characters is proving to be difficult for me, and I'd like to ask for help.
I understand a program can't be written just to detect digits and reject unacceptable characters. Conversion is needed, but here's where I'm getting stuck. Say if I wanted to input 51.50 versus 51.505-- what are the differences in the character conversions? How are the steps going to differ? Is using a loop for each element appropriate for C? Thanks!
|

October 14th, 2012, 01:56 AM
|
 |
Contributed User
|
|
|
|
Use strtod.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
char input[] = "1234.5678\n";
char *end;
double result = strtod(input,&end);
printf("Status of conversion=%d\n", errno);
printf("Result=%f\n", result);
printf("String length of number=%d\n", (int)(end-input));
printf("Following character=%d\n", *end);
return 0;
}
$ gcc foo.c
$ ./a.out
Status of conversion=0
Result=1234.567800
String length of number=9
Following character=10
You should be able to pretty much nail down anything you want to know about your decimal input.
|

October 14th, 2012, 02:01 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
|
Is this more efficient than using isdigit as a function?
|

October 14th, 2012, 02:24 PM
|
 |
Contributed User
|
|
|
|
|
> Is this more efficient than using isdigit as a function?
Given the limiting speed of any stream of I/O to be able to deliver characters to your program, probably not.
Also, how long is it going to take you to develop the code using isdigit?
You might shave a few microseconds per validation, but if it takes you 3 days to write and debug your new approach, is it that much of a bargain?
|

October 14th, 2012, 02:56 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem > Is this more efficient than using isdigit as a function?
Given the limiting speed of any stream of I/O to be able to deliver characters to your program, probably not.
Also, how long is it going to take you to develop the code using isdigit?
You might shave a few microseconds per validation, but if it takes you 3 days to write and debug your new approach, is it that much of a bargain? |
The reason I ask is because my C programming teacher is set on the idea that certain functions can make a code look "cleaner" and be more efficient. I know developing the code would take me longer, if not just as long. I did say I'm a bit of a doorknob...
|

October 14th, 2012, 03:00 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
|
I just want to make sure I understand something-- if I enter the number "48," it converts to 52 and 56 in ASCII, correct?
|

October 14th, 2012, 03:14 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by redsumac I just want to make sure I understand something-- if I enter the number "48," it converts to 52 and 56 in ASCII, correct? |
Yes that's correct for ascii based systems. If you want a portable program then you should check out the functionality in ctype.h...
Code:
int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
|

October 14th, 2012, 03:19 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
|
I was wondering about that. Could I use errno.h and ctype.h?
|

October 14th, 2012, 03:28 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by redsumac I was wondering about that. Could I use errno.h and ctype.h? |
Well you can use them together in the same program but the functionality in ctype.h doesn't set the errno.
|

October 14th, 2012, 06:13 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
|
I could use isdigit with ctype.h, because I do want portability.
|

October 15th, 2012, 01:28 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by redsumac Say if I wanted to input 51.50 versus 51.505-- what are the differences in the character conversions? How are the steps going to differ? Is using a loop for each element appropriate for C? Thanks! | Then strtod is your friend. Quote: | Originally Posted by redsumac I could use isdigit with ctype.h, because I do want portability. | That's fine if you want to reimplement the functionality of something like strtod. Just keep in mind that it's going to be a lot more complicated than the version using strtod.
|

October 15th, 2012, 05:07 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Lux Perpetua Then strtod is your friend. That's fine if you want to reimplement the functionality of something like strtod. Just keep in mind that it's going to be a lot more complicated than the version using strtod. |
Could you elaborate? I know some of the programs I've seen include a couple of functions which is complicated enough, so the cleaner I could get it, the better I suppose.
|

October 15th, 2012, 11:54 PM
|
 |
Contributed User
|
|
|
|
|
> Could you elaborate? I know some of the programs I've seen include a couple of functions which is complicated enough,
> so the cleaner I could get it, the better I suppose.
How much simpler that post #2 do you want it to be?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|