Shashank Mishra
Shashank Mishra

Reputation: 19

How to extract integers from array of strings in C++

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}

Upvotes: 1

Views: 732

Answers (4)

Udit Kr. Arya
Udit Kr. Arya

Reputation: 26

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 

Upvotes: 1

Onitz
Onitz

Reputation: 46

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).

Upvotes: 0

Xiaotian Pei
Xiaotian Pei

Reputation: 3260

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;
}

Upvotes: 0

Michael Albers
Michael Albers

Reputation: 3779

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.

Upvotes: 0

Related Questions