Pointers and Functions with Arrays

So, I think I've got this mostly figured out but I'm receiving two error codes when I attempt to compile this code. The assignment specifies that we are not to use any functions from the cstring library. I am unsure about how to go about the code for the last two functions to append str2 and str3 to str1 but I gave it a shot...the error messages I am receiving are:

1
2
1> error LNK2019: unresolved external symbol "int __cdecl stringLength1(char *)" (?stringLength1@@YAHPAD@Z) referenced in function _main
1> fatal error LNK1120: 1 unresolved externals


Any direction would be much appreciated, thanks so much!

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 #include <iostream>
using namespace std;

// Implement the following function prototypes
int   stringLength1 (char *str);
int   stringLength2 (char *str);
char* stringCopy1 (char *str1, const char *str2);
char* stringCopy2 (char *str1, const char *str3);
char* stringCat1 (char *str1, const char *str2);
char* stringCat2 (char *str1, const char *str3);

// Use the following driver to test implementations:
int main()
{
	char  str1[20];
	char* str2 = "Forrest";
	char* str3 = "Run";
  
	// Test stringLength functions
   
	cout << "stringLength1 (" << str2 << "): "
		<< stringLength1 (str2) << endl;
	cout << "stringLength2 (" << str3 << "): "
		<< stringLength2 (str3) << endl;

	// Test stringCopy functions
   
	cout << "stringCopy1 (str1, " << str2 << "): "
		<< stringCopy1 (str1, str2) << endl;
	cout << "stringCopy2 (str1, " << str3 << "): "
		<< stringCopy2 (str1, str3) << endl;
 
	// Test stringCat functions
   
	cout << "stringCat1 (str1, " << str2 << "): "
		<< stringCat1 (str1, str2) << endl;
	cout << "stringCat2 (str1, " << str3 << "): "
		<< stringCat2 (str1, str3) << endl;
 
	return 0; 
} // end main

int stringLength1(const char* str)
{
	int counter = 0;
		
		while(str[counter] != '\0')
		{
			counter++;
		}
	
	return counter;
}

int   stringLength2 (char *str)
{
	int counter = 0;
		
		while(str[counter] != '\0')
		{
			counter++;
		}
	
	return counter;
}

char* stringCopy1 (char *str1, const char *str2)
{
	*str1 = *str2;

	return str1;
}

char* stringCopy2 (char *str1, const char *str3)
{
	*str1 = *str3;

	return str1;
}

char* stringCat1 (char *str1, const char *str2)
{
	int size = stringLength1(str2);
	char *null = new char[stringLength1(str2) + 1];

	null[stringLength1(str2)] = size;
	null[stringLength1(str2) + 1] = '\0';
  
	return null;
}

char* stringCat2 (char *str1, const char *str3)
{
	int size = stringLength1(str3);
	char *null = new char[stringLength1(str3) + 1];

	null[stringLength1(str3)] = size;
	null[stringLength1(str3) + 1] = '\0';
  
	return null;
}
The stringLength1 function signatures don't match.
1
2
int   stringLength1 (char *str);
int stringLength1(const char* str)
Oh lord ok, got that that was a total oversight on my part... well it compiles but now all I get is garbage back.... the out put I'm looking for is:

stringLength1 (Forrest): 7
stringLength2 (Run): 3
stringCopy1 (str1, Forrest): Forrest
stringCopy2 (str1, Run): Run
stringCat1 (str1, Forrest): RunForrest
stringCat2 (str1, Run): RunForrestRun

stringLenth1 and 2 return correctly but I get a bunch of nonsense for the remaining 4...
Alrighty I've worked with it some and got the 2nd pair of functions to give me the correct output, but the final two functions break.

My code so far:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;

// Implement the following function prototypes
int   stringLength1 (const char *str);
int   stringLength2 (const char *str);
char* stringCopy1 (char *str1, char *str2);
char* stringCopy2 (char *str1, char *str3);
char* stringCat1 (char *str1, char *str2);
char* stringCat2 (char *str1, char *str3);

// Use the following driver to test implementations:
int main()
{
	char  str1[20];
	char* str2 = "Forrest";
	char* str3 = "Run";
  
	// Test stringLength functions
   
	cout << "stringLength1 (" << str2 << "): "
		<< stringLength1 (str2) << endl;
	cout << "stringLength2 (" << str3 << "): "
		<< stringLength2 (str3) << endl;

	// Test stringCopy functions
   
	cout << "stringCopy1 (str1, " << str2 << "): "
		<< stringCopy1 (str1, str2) << endl;
	cout << "stringCopy2 (str1, " << str3 << "): "
		<< stringCopy2 (str1, str3) << endl;
 
	// Test stringCat functions
   
	cout << "stringCat1 (str1, " << str2 << "): "
		<< stringCat1 (str1, str2) << endl;
	cout << "stringCat2 (str1, " << str3 << "): "
		<< stringCat2 (str1, str3) << endl;
 
	system("pause");
	return 0; 
} // end main


//Determines the length of string str. The number of characters preceding the terminating null character is returned.
int stringLength1(const char* str)
{
	int counter = 0;
		
		while(str[counter] != '\0')
		{
			counter++;
		}
	
	return counter;
}

int   stringLength2 (const char *str)
{
	int counter = 0;
		
		while(str[counter] != '\0')
		{
			counter++;
		}
	
	return counter;
}

//Copies the string str2 into the character array str1. The value of str1 is returned.
char* stringCopy1 (char *str1, char *str2)
{
	str1 = str2;

	return str1;
}

char* stringCopy2 (char *str1, char *str3)
{
	str1 = str3;

 	return str1;
}

//Appends a copy of string str2 to string str1. The first character of str2 overwrites the terminating null character of str1. The value of str1 is returned.
char* stringCat1 (char *str1, char *str2)
{
	char *cat = '\0';
	
	*cat = *str1 + *str2;

	return cat;
}

char* stringCat2 (char *str1, char *str3)
{
	char *cat = '\0';
	
	*cat = *str1 + *str3;

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