Manually Concatenating two C-Strings

Thanks a lot for the assistance earlier with my mjcStrCpy function. Got a question in regards to the concatenation function I'm trying to write now though. I got it to work pretty much. My only problem is that I can't assign the new string I created in the function, which contains the other two, back to the first string. He doesn't want us to output it within the function, and I can't return it either because the prototype he's making us use is a void return-type function. So I don't know of how else to retain the new, concatenated string.

Only the second argument to the function (second string) is a const pointer, so I should be able to change what the first one can point to. Please see my notes below, and once again thank you so much in advance!

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
// Author: Matt
// File: mjccstring.cpp
// Operating System: Linux
// Compiler: g++
// Written: 2/2013
// Assigned: 2/7/2013
// Due: 2/15/2013
// Purpose: 	This program will take two strings as input from the user, and perform various C-String related tasks with them

#include <iostream>
#include <iomanip>

// mjccstring.cpp
//−−−−−−−−−−−−−−−−
// (3 pts) Use pointer arithmetic (no subscripts).
// Return the number of characters before the null
// Example: iiiStrLen("xyz") would return 3
int mjcStrLen(const char * const);

// (5 pts) Use subscripts.
// Concatenate (append) the second string onto the end of the first one
// (assume that the first has appropriate space)
// example:
//   first string = "abc"
//   second string = "def"
//   resulting string = "abcdef"
void mjcStrCat(char [], const char []);

//initializes variables and calls the other functions
int main()
{
	char cstring1[] = "This is the first string, ";
	char cstring2[] = "and this is the second string";
	std::cout << "C-String 1 is:  " << cstring1 << std::endl;
	std::cout << "C-String 2 is:  " << cstring2 << std::endl;
	std::cout <<  cstring1 << " contains " << mjcStrLen(cstring1) << " characters" << std::endl;
	std::cout <<  cstring2 << " contains " << mjcStrLen(cstring2) << " characters" << std::endl;
	mjcStrCat(cstring1, cstring2);
	std::cout << "In main, after mjcStrCat exits, C-String 1 is:  " << cstring1 << std::endl;
	return 0;
}

//uses a while loop to count the number of characters in a C-string
int mjcStrLen(const char * const test_string)
{
	int length = 0;
	int index = 0;
	while(*(test_string + index)) //will be true until it hits the null character
	{
		length++;
		index++;
	}
	return length;
}

//concatenates string1 and string2 into a new string, then string1 will point to it
void mjcStrCat(char test_string1[], const char test_string2[])
{
	//gathers length of both strings, and creates a new string with the necessary space
	int size_of_string1 = mjcStrLen(test_string1);
	int size_of_string2 = mjcStrLen(test_string2);
	int size_of_newstring = size_of_string1 + size_of_string2;
	char new_string[size_of_newstring];
	//copy both strings to new string
	int index_of_newstring = 0;
	for(index_of_newstring; index_of_newstring < size_of_string1; index_of_newstring++)
		new_string[index_of_newstring] = test_string1[index_of_newstring];
	for(int index_of_string2 = 0; index_of_string2 < size_of_string2; index_of_string2++)
	{
		new_string[index_of_newstring] = test_string2[index_of_string2];
		index_of_newstring++;
	}
	//pointing first string to new string
	std::cout << "New String is:  " << new_string << std::endl;
	std::cout << "Inside function, before assignment, C-String 1 is:  " << test_string1 << std::endl;
	test_string1 = new_string;
	std::cout << "Inside function, after assignment, C-String 1 is:  " << test_string1 << std::endl;
}
Line 63: char new_string[size_of_newstring]; — non-standart extention to language, will not work everywhere, and you will get point deducted (probably) for this. Also it is a local variable which will be destroed when function ends.
Do dynamic memory allocation: char new_string = new char[size_of_newstring]
then this: test_string1 = new_string; should do the trick.

BTW you can simplify your length function:
1
2
3
4
5
6
int mjcStrLen(const char * const test_string)
{
	int length = 0;
	while(test_string[length++]) ;
	return length;
}
Thanks. I finally got something that works. May or may not be the most efficient, but when something's two weeks past due, I'll take it you know? Once again, appreciate the help

1
2
3
4
5
6
7
8
9
10
11
12
void mjcStrCat(char test_string1[], const char test_string2[])
{
	//begin operation at the end of string1 and the beginning of string2
	int index_of_string1 = mjcStrLen(test_string1);
	int index_of_string2 = 0;
	//looping through string2 and assigning it's characters to the end of string1
	for(index_of_string1; index_of_string1 < 256; index_of_string1++)
	{
		test_string1[index_of_string1] = test_string2[index_of_string2];
		index_of_string2++;
	}
}
Topic archived. No new replies allowed.