Forgive me for my naivety I'm just learning C for the first time. Basically I have a series of strings that contain a timestamp in the format "HH:MM:SS". I'm looking to write a function of the form int tsconvert(char *) that can covert the timestamp to an integer. Here is some code I've written so far
int tsconvert(char *timestamp)
{
int x;
removeColon(timestamp,8);
x = atoi(timestamp);
return x;
}
void removeColon(char *str1, int len)
{
int j = 0;
for (int i = 0; i < len; i++)
{
if (str1[i] == ':')
{
continue;
}
else
{
str1[j] = str1[i];
j++;
}
}
str1[j] = '\0';
}
When I try and use this code however I get a segmentation error. Some one in my programing class suggested that I simply extract the numbers from the timestamp and place those in a new string but I'm not sure how to do that.
To extract the numbers from the timestamp (HH:MM:SS), just use sscanf():
const char *str = "01:02:03";
int h, m, s;
sscanf(str, "%d:%d:%d", &h, &m, &s);
printf ("%d, %d, %d\n", h, m, s);