Interleaving Characters

My code is supposed to interleave two arrays of chars. I think I have nailed it down. I feel that the way I am doing it might be a bit to complex so if anyone has pointers on how I can simplify my code that would be great. But my main question is when I run the code I get a runtime error.

"Exception thrown at 0x00F624A1 in "assignment.exe": 0xC0000005: Access violation reading location 0x00000005.

If there is a handler for this exception, the program may be safely continued."

I'm new to C++ and trying to learn but I am lost at why it gives me this. Here is the function that is supposed to interleave the arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 char* strinterleave(const char *a, const char *b) {
	const size_t lena = strlen(a);
	const size_t lenb = strlen(b);
	const size_t finallen = lena + lenb + 1;
	char *interleave = new char[finallen];

	interleave[finallen] = '\0';
	if (lena > lenb) {
		interleave[0] = a[0];
		interleave[1] = b[0];
		for (int i = 1; i < lenb; i++) {
			interleave[i * 2] = a[i];
			interleave[(i * 2) + 1] = b[i];
		}
		for (int j = (lenb * 2); j < finallen; j++) {
			if (j % 2 == 0) {
				interleave[j] = a[j / 2];
			}
			else {
				interleave[j] = a[(j + 1) / 2];
			}

		}


	}
	if (lenb < lena) {
		interleave[0] = a[0];
		interleave[1] = b[0];
		for (int i = 1; i < lenb; i++) {
			interleave[i * 2] = a[i];
			interleave[(i * 2) + 1] = b[i];
		}
		for (int j = (lena * 2); j < finallen; j++) {
			if (j % 2 == 0) {
				interleave[j] = b[j / 2];
			}
			else {
				interleave[j] = b[(j + 1) / 2];
			}

		}


	}
	if (lenb == lena) {
		interleave[0] = a[0];
		interleave[1] = b[0];
		for (int i = 1; i < lenb; i++) {
			interleave[i * 2] = a[i];
			interleave[(i * 2) + 1] = b[i];
		}
		return interleave;
		delete[] interleave;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char* cstr_interleave( const char* a, const char* b ) {

    if( a == nullptr || b == nullptr ) return nullptr ;

    const std::size_t len_a = std::strlen(a) ;
    const std::size_t len_b = std::strlen(b) ;
    if( len_a == 0 || len_b == 0 ) return nullptr ;

    const std::size_t max_len = std::max( len_a, len_b ) ;

    char* interleaved = new char[ max_len*2 + 1 ] ;

    for( std::size_t i = 0 ; i < max_len ; ++i )
    {
        interleaved[ i*2 ] = a[ i%len_a ] ;
        interleaved[ i*2 + 1 ] = b[ i%len_b ] ;
    }

    interleaved[ max_len*2 ] = 0 ; // null terminate
    return interleaved ;
}
Last edited on
Thank you for replying. My only issue that I am a bit lost on is why are you taking the modulo of the count and the length of the arrays? In my mind this only works for arrays that are less than 10 indexes long anything bigger and the index will reset back to 0.

Edit: My bad I redid the math and kind of see where you are going with it.
Last edited on
interleave[finallen] = '\0';

you're writing to the wrong place. try finallen - 1.
Topic archived. No new replies allowed.