Appending pointers and arrays

Hello, so I've been having trouble finishing this code as I'm not entirely sure how to go about the last two stringCat functions. If anyone can't point me in the right direction that'd be much appreciated. The final two functions break the code.

Professor specifically said we can not use cstring functions...
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;
}
Last edited on
Editing the last two functions a bit and got it to compile without breaking, however I'm just receiving garbage code as my result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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)
{

  	*str1 =  *str1 + *str2;

  return str1;
}

char* stringCat2 (char *str1, char *str3)
{

  	*str1 = *str1 + *str3;
 
  return str1;
}
closed account (D80DSL3A)
I afraid your stringCopy functions don't actually work either, although they may appear to work using just the driver code. The functions just return a pointer to the second string. Nothing is actually copied.
To see this, insert this code after line 31 in your program:
cout << "str1 actually contains: " << str1 << endl;

Why are there 2 stringCopy and 2 stringCat functions? How are they supposed to differ?

To copy from one string to another you need to copy the individual characters over.
Code similar to that in your stringLength functions will do:
1
2
3
4
5
6
7
8
int counter = 0;
		
while(str2[counter] != '\0')
{
        str1[counter] = str2[counter];// copy each character value
	counter++;
}
 str1[counter] = '\0';// add null terminator 

A similar strategy should work for the stringCat function, except you are copying onto the end of str1. Use an offset to the end of str1:
str1[counter + ?] = str2[counter];
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
char* stringCat1 (char *str1, char *str2)
{
        int strLen1;
        int strLen2;
        for (strLen1=0; str1!='\0'; strLen1++);
	for (strLen2=0; str2!='\0'; strLen2++);
        char *cat=new char[strLen1+strLen2+1];
        for (int i=0; i<strLen1; i++)
                cat[i]=str1[i];
        for (int i=0; i<strLen2+1; i++)
                cat[i+strLen1]=str2[i];
	return cat;
}


this is what i would do... if someone knows of a more efficient way, tell me... I would like to know
Last edited on
Topic archived. No new replies allowed.