Implementing a Concatenates Function

I am having trouble writing a program that uses two helper functions that concatenates the strings a and b to the buffer result. It can hold result_maxlength + 1 bptes available and provide a '\0' terminator. This is what I have so far..
1. I know my strcat function is off
2. I don't know how to use those two functions in my concatenates function
3. Why is giving me errors for these two lines say BFSZ1 and BFSZ2 must have a constant value.
char c[BFSZ1 + 1];
char d[BFSZ2 + 1];

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
#include <iostream>

using namespace std;

char *strcat( char *s, const char *t)
{
	 char * d = s;
	 while (*d++);
	 char const * a = t;
		while (*a) { *d++ = *a++; }
			*d = 0;
	 return s;
}

char *mystrncpy(char *s, const char *t, int n)
{
	for(int i = 0; t[i] != '\0'; ++i)
	{
		s[i] = t[i];
	}
	return s;
}


void concat(const char a[], const char b[], char result[], int result_maxlength)
{
	// use strncpy and strncat
}

int main()
{
   int BFSZ1 = 20;
   char c[BFSZ1 + 1];
   int BFSZ2 = 100;
   char d[BFSZ2 + 1];
   char a[] = "hjhjhkfhjhj";
   char b[] = "ryeioyeiytuiy";
   concat(a,b,c,BFSZ1);
   cout << c << "\n";
   concat(a,b,d,BFSZ2);
   cout << d << "\n";
}

Your strcpy looks ok.

concat should:
1. copy a to result (using result_maxlenth as the boundary).
2. add b to result (using result_maxlenth as the boundary).

Last edited on
When you declare an array the thing in [] has to be constant.
1
2
3
4
5
6
7
int a[5];//ok

const int B = 7;
int b[B+1];//ok

int C = 10;
int c[C];//wrong 
should it look like..

create temp variable
loop through the elements up to maxlength
copy a to result
return result + b

@hamsterman what do you mean? I'm confused where your saying my error is.

Topic archived. No new replies allowed.