C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 14th, 2012, 01:36 AM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old October 14th, 2012, 01:56 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old October 14th, 2012, 02:01 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 19 sec
Reputation Power: 0
Is this more efficient than using isdigit as a function?

Reply With Quote
  #4  
Old October 14th, 2012, 02:24 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> 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?

Reply With Quote
  #5  
Old October 14th, 2012, 02:56 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #6  
Old October 14th, 2012, 03:00 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #7  
Old October 14th, 2012, 03:14 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);

Reply With Quote
  #8  
Old October 14th, 2012, 03:19 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #9  
Old October 14th, 2012, 03:28 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old October 14th, 2012, 06:13 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old October 15th, 2012, 01:28 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.

Reply With Quote
  #12  
Old October 15th, 2012, 05:07 PM
redsumac redsumac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 redsumac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #13  
Old October 15th, 2012, 11:54 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> 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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Testing characters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap