LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2013, 07:38 AM   #1
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Rep: Reputation: Disabled
Parsing String


how can we validate a date of birth.is there any available c library function for parsing a date of the format 09-03-1989
 
Old 06-26-2013, 12:18 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
There are a couple of ways; however, have you tried AWK to do this? That, of course, presupposes that you can validate prior to a C program which may not be feasible.

So, parsing functions in C.

You can simply read the field as a string into a buffer then take it apart with pointers to the three fields.

09 will be the first two positions of buf (0 and 1), 03 will be in buf (3 and 4) and 1989 will be from buf (6 - 9). The strncpu() function comes in handy for that; the strncpy()function is defined as:
Code:
char *strncpy(char *dest, const char *src, size_t n);
Here's a quick-and-dirty way of doing it:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void    main    (void)
{
        char    buf [] = { "09-03-1989" };
        char    mm [3];
        char    dd [3];
        char    yy [5];

        /*      display what we're looking at   */
        (void) fprintf (stdout, "%s\n", buf);
        /*      extract month from buf          */
        (void) strncpy (mm, buf, 2);
        mm [2] = '\0';
        /*      extract day of month from buf   */
        (void) strncpy (dd, buf + 3, 2);
        dd [2] = '\0';
        /*      extract year from buf           */
        (void) strncpy (yy, buf + 6, 4);
        yy [4] = '\0';
        /*      display what we did             */
        (void) fprintf (stdout, "%s %s %s\n", mm, dd, yy);
        /*
         *      from here, convert mm, dd, yy
         *      to integers and validate them
        */
        if (atoi (mm) < 1 || atoi (mm) > 12) {
                (void) fprintf (stderr, "Month [%s] is invalid\n", mm);
                exit (EXIT_FAILURE);
        }
        exit (EXIT_SUCCESS);
}
I'll leave it to you to validate the day of the month taking into account the number of days in a given month and February and leap years.

Now, this could be a function, called by a program.

You could convert strings to numeric with the strtol() rather than atoi()function; e.g.,
Code:
dd = (int) strtol (day, (char **) NULL, 10);
yy = (int) strtol (yer, (char **) NULL, 10);
Another way to parse is with the strtok() function; here's a small example of how to do that:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void	main	(void)
{
	char	buf [BUFSIZ];
	char	*tokptr, *strptr = buf;

	(void) gets (buf);
        /*     the dash is the token pointer here     */
	while ((tokptr = strtok (strptr, "-\t")) != (char *) NULL) {
		(void) fprintf (stdout, "%s\n", tokptr);
		strptr = (char *) NULL;
	}
	exit (EXIT_SUCCESS);
}
Execute it:
Code:
propg
03-09-1989
03
09
1989
There are all sorts of other ways, too, but the above are simple, efficient (within the confines you gave) and will work for you.

Hope this helps some.
 
1 members found this post helpful.
Old 06-26-2013, 12:56 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Also strptime: http://linux.die.net/man/3/strptime
 
1 members found this post helpful.
Old 06-26-2013, 01:14 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by NevemTeve View Post
That's one I didn't know about -- looks like a good possiblity!
 
  


Reply

Tags
string



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Probably String Parsing or Something jroggow Programming 4 01-27-2013 01:53 PM
[SOLVED] Parsing mountpoint from /etc/mtab (parsing "string") in C deadeyes Programming 3 09-06-2011 05:33 PM
Parsing String. msgforsunil Linux - Newbie 8 06-11-2009 01:47 PM
string parsing using perl bharatbsharma Programming 1 12-05-2007 06:04 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration