Thread: For Loop leaves C-String untouched

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,754
    Quote Originally Posted by SuchyTV
    (I always need 8 characters + ''\0")
    Then malloc(9), or if you want to use your "00:00:00" as a "template", then malloc(sizeof("00:00:00")) will work. I don't see why you are confused.

    Quote Originally Posted by SuchyTV
    Then I need to take the first characters of s, modify them and put them into military_time. I am not allowed to modify directly. Is there a method for substitution?
    If you cannot see that you can modify while copying, or copy first then modify the copy...

    Quote Originally Posted by SuchyTV
    However I need a way to copy from biggerString to smallerString with thereby setting '\0' at the end of the smaller string.
    Then you are mistaken: you are not "wasting space" because you must allocate that amount of space in order to copy the bigger source string.

    What you can do is to waste time and additional temporary space by copying to a final dynamic array that is 9 characters in size then return that, but that's just silly.

    EDIT: looking at your original code in post #1, you're just confusing yourself (and me) by being worried by the length of s. Allocate with malloc(9) or malloc(sizeof("00:00:00")).
    Last edited by laserlight; 1 Week Ago at 04:54 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Feb 2019
    Posts
    302
    What about this?
    Code:
    #define _XOPEN_SOURCE
    #include <stdio.h>
    #include <time.h>
    
    char *time_conversion( char *t )
    {
      static char s[9];
      struct tm tm;
    
      strptime( t, "%I:%M:%S %p", &tm ); 
    
      // this "format" (4th argument) is glibc's only!
      // use "%H:%M:%S" in other compilers...
      if ( strftime( s, sizeof s, "%02H:%02M:%02S", &tm ) )
        return s;
    
      // return NULL in case of "error".
      return NULL;
    }
    
    int main( void )
    {
      char *s = "7:23:00 PM"; // test.
    
      printf( "'%s' -> '%s'\n", s, time_conversion( s ) );  
    }

  3. #18
    Registered User
    Join Date
    Feb 2019
    Posts
    302
    Sorry... it doen't work as you expected... Here's another implementation:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    // requires a string formated as:
    //    "[h]h:mm[:ss][ am/pm]"
    char *time_conversion( char *t )
    {
      static char s[9];
      char *p, *q;
      int step = 0;
      int hr, m, sec = 0;
     
      *s = '\0';
    
      // make a copy...
      p = strdup( t );
    
      q = strtok( p, ": " );
      while ( q )
      {
        switch ( step++ )
        {
          case 0:   // hour.
            hr = atoi(q); break;
          case 1:   // minuttes
            m = atoi(q); break;
          case 2: // seconds or AM/PM.
          case 3:
            if ( ! strcasecmp( q , "PM" ) )
            {
              if ( hr < 12 )
                hr += 12;
              break;
            }
    
            if ( ! strcasecmp( q, "AM" ) )
            {
              if ( hr == 12 )
                hr = 0;
              break;
            }
    
            sec = atoi(q);
            break;
    
          // ignore everything else...
          default:
            goto exit_loop;
        }
    
        q = strtok(NULL, ": " );
      }
    
    exit_loop:
      free( p );
    
      sprintf( s, "%02d:%02d:%02d", hr, m, sec );
     
      return s;
    }
     
    int main( void )
    {
      char *s[] = { "0:00", "12:00 AM", "11:59:59 AM", "10:23", "12:00", "12:00:00 PM", "7:23 PM", "8:01:03 PM", NULL }; // tests.
      char **p;
     
      p = s;
      while ( *p )
      {
        printf( "%-13s -> %s\n", *p, time_conversion( *p ) );  
        p++;
      }
    }
    Testing:
    Code:
    $ cc -o test test.c
    $ ./test
    0:00          -> 00:00:00
    12:00 AM      -> 00:00:00
    11:59:59 AM   -> 11:59:59
    10:23         -> 10:23:00
    12:00         -> 12:00:00
    12:00:00 PM   -> 12:00:00
    7:23 PM       -> 19:23:00
    8:01:03 PM    -> 20:01:03

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2009, 12:11 PM
  2. leaves on a binary tree
    By drdodirty2002 in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2004, 04:03 AM
  3. Calculates the number of leaves
    By NightWalker in forum C Programming
    Replies: 4
    Last Post: 09-15-2003, 03:02 AM
  4. SDL project leaves trail
    By Shadow12345 in forum Game Programming
    Replies: 6
    Last Post: 05-26-2002, 09:19 AM
  5. my prog leaves fast
    By xlordt in forum C Programming
    Replies: 7
    Last Post: 01-03-2002, 06:29 PM

Tags for this Thread