Thread: string tokenizing string tok

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    string tokenizing string tok

    Hey guys just wondering if i could get some help tokenizing a date string in format of dd/mm/yyyy. I some how need put the users entered date of birth into seperate variables. Day,month, and year watching out for the forward slash. Then somehow cast the string tokens day,month,year into ints how would i do that?





    Code:
    char buff[BUFSIZ];
     char delims[] = "/";
     char *result = NULL;
     struct tm t;
     time_t now;
    
     char date [MAXDATE]; /* buffer size is 11 */
     int valid = 0;
     int day,month,year;  
     /* declaration of variables */
    
     time(&now); 
     /* put now into time */
    
     t = *localtime(&now); 
     /* get the current date */
    
     strftime(buff, sizeof(buff), "%d//%m//%y", &t); 
     /* format the date*/
      
    
     /* --- printing the date ---- */
     printf("The current date is %s\n",buff);
     printf("\nPlease Enter your age in dd/mm/yyyy format:");
    
    
     fgets(date,MAXDATE,stdin); 
     /* getting user input date */
     
     do
     {
       fgets(date,MAXDATE,stdin);
    
       result = strtok( date, delims );
      /* skipping the / in date and putting it inside variable char result*/
    
        while( result != NULL ) {
        printf( "result is \"%s\"\n", result );
        result = strtok( NULL, delims );

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's kind of a long way to do it. Since the format is fixed, why not use sscanf and a validation routine?
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int is_valid ( int m, int d, int y )
    {
      static const int  mday[] = {
        0,31,28,31,30,31,30,31,31,30,31,30,31
      };
    
      if ( m <= 0 || m > 12 )
        return 0;
      else if ( d <= 0 || d > mday[m] )
        return 0;
      else if ( y < 1900 || y > 3000 ) /* Arbitrary choices */
        return 0;
    
      return 1;
    }
    
    int main ( void )
    {
      char s[20];
      int m, d, y;
    
      if ( fgets ( s, sizeof s, stdin ) != NULL ) {
        if ( sscanf ( s, "%d/%d/%d", &m, &d, &y ) == 3 ) {
          if ( is_valid ( m, d, y ) )
            printf ( "%d/%d/%d is a valid date\n", m, d, y );
          else
            printf ( "Invalid date\n" );
        }
      }
    }
    My best code is written with the delete key.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    could you show an alternative way as well

    okay thats great. Thanks for that... Just wondering if you could show me how to do it the long way as well so then atleast i can get a good grasp on a few ways of doing it..



    thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It might look something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int is_valid ( int m, int d, int y )
    {
      static const int  mday[] = {
        0,31,28,31,30,31,30,31,31,30,31,30,31
      };
    
      if ( m <= 0 || m > 12 )
        return 0;
      else if ( d <= 0 || d > mday[m] )
        return 0;
      else if ( y < 1900 || y > 3000 ) /* Arbitrary choices */
        return 0;
    
      return 1;
    }
    
    int main ( void )
    {
      char s[20];
      int date[3];
    
      if ( fgets ( s, sizeof s, stdin ) != NULL ) {
        char *t, *end;
        int i;
    
        /* Remove newline */
        s[strcspn ( s, "\n" )] = '\0';
    
        t = strtok ( s, "/" );
    
        for ( i = 0; i < 3 && t != NULL; i++ ) {
          date[i] = strtol ( t, &end, 0 );
    
          if ( date[i] == 0 && *end != '\0' ) {
            fprintf ( stderr, "Invalid format\n" );
            return EXIT_FAILURE;
          }
    
          t = strtok ( NULL, "/" );
        }
    
        if ( i != 3 )
          printf ( "Invalid format\n" );
        else if ( is_valid ( date[0], date[1], date[2] ) )
          printf ( "%d/%d/%d is valid\n", date[0], date[1], date[2] );
        else
          printf ( "Invalid date\n" );
      }
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM