1

Given an array of the form

char* timePeriod= {"6AM#8AM","11AM#1PM","7AM#3PM","7AM#10AM","10AM#12PM"};

how can I extract the start and end time in integer arrays of following form:

start_time={6,11,7,7,10}
end_time={8,1,3,10,12}
1

4 Answers 4

1

You can use "sscanf" to do so. And don't forget to mark it as useful :)

#include <stdio.h>
int main() 
{
    int a,b,i;
    int start_time[5], end_time[5];

    char *t[5] = {"6AM#8AM","11AM#1PM","7AM#3PM","7AM#10AM","10AM#12PM"};
    char *(*ptr)[5] = &t;

    for(i=0;i<5;i++)
    {
        sscanf((*ptr)[i], "%d%*[AP]M#%d%*[AP]M", &a, &b);
        start_time[i]=a;
        end_time[i]=b;
    }
    printf("Starting time : ");
    for(i=0;i<5;i++)
    {
        printf("%d ",start_time[i]);
    }
    printf("\nEnding time : ");
    for(i=0;i<5;i++)
    {
        printf("%d ",end_time[i]);
    }  
    return 0;
}
OUTPUT:
Starting time : 6 11 7 7 10 
Ending time : 8 1 3 10 12 
Sign up to request clarification or add additional context in comments.

Comments

0

A pretty simple way would be to use strtok to split the string on the #, then use atoi on each piece. It will stop once it sees a non-numeric value.

Comments

0

sscanf is capable of doing this, although I don't think it's a very good way. look at here for details

#include <stdio.h>
int main() {
  const char* t = "6PM#8AM";
  int a, b;
  sscanf(t, "%d%*[AP]M#%d%*[AP]M", &a, &b);
  printf("%d %d\n", a, b);
  return 0;
}

or you can regex in C++11

#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
  string t("6PM#8AM");
  smatch match;
  regex re("([[:digit:]])+[AP]M#([[:digit:]])+[AP]M");
  if (regex_match(t, match, re)) {
    cout << match[1].str() << " " << match[2].str() << '\n';
  }
  return 0;
}

Comments

0

Note: the following solution makes a few very specific assumptions about your dataset (if these are not the case, you may prefer using a regex).

  1. The string will always start with integers
  2. The endtimes will always either be in the 4th or 5th index of your string
  3. Zero is not a valid time

#include <iostream>
#include <string>

int main() {

    std::string timePeriod[5] = {"6AM#8AM","11AM#1PM","7AM#3PM","7AM#10AM","10AM#12PM"};
    int   startTimes[5] = {0,0,0,0,0};
    int     endTimes[5] = {0,0,0,0,0};

    for(int i=0;i<5;i++) {

        std::string s = timePeriod[i];
        startTimes[i] = atoi(s.c_str());             // Convert first number to integer
        endTimes[i]   = atoi(s.substr(4).c_str());   // Convert 2nd numbers to integer
        if(endTimes[i] == 0)                         // If zero
            endTimes[i] = atoi(s.substr(5).c_str()); // Get the 5th index instead

        std::cout << "Start: " << startTimes[i] << "\t End: " << endTimes[i] << std::endl;
    }
}

The lack of leading zeros makes it a bit trickier, but ultimately - it can be quickly solved using substrings (assuming also you don't mind changing the char* into a std::string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.