0

I want to split a char* time = "15:18:13"; into char* hour;, char* minute; and char* seconds;.

The problem is that I don't know how. I am like to try a Pebble Watchface. I already used char* hour = strok(time, ":"); but the first parameter needs to be a char[] but time is a char*.

Does anyone know how to do this?

Maroun
  • 94,125
  • 30
  • 188
  • 241
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • 2
    `strtok_s` does what you want, iff the input may be modified. String literals are constants, even though they have type `char[]` for historic reasons. You might need a `strdup` first (`strlen`+`malloc`+`memcpy`). Anyway, in your case a simple scan to numbers using `sscanf(s, "%d:%d:%d",...)` might be best. – Deduplicator May 01 '14 at 13:27
  • Use Data Object don't split it !!! – Ran Adler May 01 '14 at 13:28
  • If you use `strdup`, I suggest you implement your own, since it is not standard! ;) – gsamaras May 01 '14 at 13:30
  • Did you read the man page, especially the BUGS section? – alk May 01 '14 at 13:33
  • If it's about scanning/parsing time/date strings you might like to use the `strptime()` function. – alk May 01 '14 at 13:40
  • strftime is the best way – zszen Jul 11 '16 at 16:32

2 Answers2

2

Based on alk's comment, the approach one can use is sscanf, like this:

#include <string.h>

int main ()
{
  char* str = "15:18:13";
  int a, b, c;
  sscanf(str, "%d:%d:%d", &a, &b, &c);
  printf("%d %d %d\n", a, b, c);
  return 0;
}

However, the following is a more general solution.

Use strtok. You can store them in an array, like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
  char str[] ="15:18:13";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,":");
  char* a[3];
  int i = 0;
  while (pch != NULL)
  {
    a[i] = malloc( (strlen(pch)+1) * sizeof(char));
    strcpy(a[i++], pch);
    printf ("%s\n",pch);
    pch = strtok (NULL, ":");
  }

  for(i = 0 ; i < 3 ; i++)
    printf ("%s\n",a[i]);

  return 0;
}

strdup, as suggested by Deduplicator, can also help, but it is not standard, thus I suggest to avoid (or implement your own, not that hard). :)

Moreover, strtok_s that Deduplicator mentions is not provided in C.

Reply to OP's comment below:

Question: str is char str[] but time is a char*. Can I convert this?

You can assign it to an array like this:

#include <stdio.h>

int main ()
{
  char* from = "15:18:13";
  char to[strlen(from) + 1]; // do not forget +1 for the null character!
  strcpy(to, from);
  printf("%s\n", to);
  return 0;
}

GIFT: I suggest you read the first answer from here.

It provides a smooth explanation to char* and char[].

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

Don't need those api. Pebble has own api for time out: strftime

tm *tmNext = localtime(&timeRequestTmp);
static char strformatForTimeNext[10];
strftime(strformatForTimeNext,10,"%H:%M",tmNext);
text_layer_set_text(layer_nextTime, strformatForTimeNext);
zszen
  • 1,428
  • 1
  • 14
  • 18