selecting a group of values in an array

Hey guys. I have a question about arrays.


If I have an 8 character array, is it possible to split the array up and, say, assign the first few values of the array to one variable, and the next few values to an different variable?

For instance, If my array has the numbers 20110202 in it, I want to split it up so I can can have it say something like "year: 2011 month: 02 day: 02"

I am fairly familiar with c++ techniques, so I dont think i'll need a complete explanation about how to write this from scratch. I just dont know how to split an array up like that.

Thanks
Well, you could, like
1
2
3
4
5
char str[] = "Hello world";
char* start = str+1, *end = str+6;

for(char* i = start; i < end && *i; i++) std::cout << *i;
//should print "ello" 
but it would be more understandable to simply copy parts into a new array.
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    char date[] = "20110202";
    char year[5] = {0};

    copy(date, date+4, year);

    cout << year << endl;

    return 0;
}
Topic archived. No new replies allowed.