str_cat

Good afternoon,

just working on something here that should use pointers to arrays and concatenate the answer.

please let me know where i went wrong:

please see updated code below

Many Thanks!
Last edited on
The names are strcpy() and strcat()
The first parameter is the destination in each case.
http://www.cplusplus.com/reference/cstring/strcat/
http://www.cplusplus.com/reference/cstring/strcpy/
You fail to follow the simple instructions...
1. Declares and initializes a character string to read as follows:
char array[80] = “To be or not to be, that is the question…..”
char brray[ ] = “Tomorrow and tomorrow creeps in this petty place …..”


This below seems to ask you to define your own custom str_cat() function.
2. Contains a function, char* str_cat(char *array_ptr, char *barray_ptr), which concatenates the second string onto the end of the first string.


1
2
char str_cpy (char *first_array, char *temp_result);
char str_cat (char *temp_result, char *second_array_ptr);


These are called prototypes, they do not define a function.
http://cplusplus.com/doc/tutorial/functions/
Good evening,

i am still off on this thing... i dont see what i am doing wrong. i have read the links and looked at the examples. they dont seem to get through to me...


please see updated code below
Last edited on
Please use code tags. You can also edit your current posts.
http://cplusplus.com/articles/jEywvCM9/

char *strcat(char *xrray_ptr, char *brray_ptr);

That is not a function call.
If you want to call the strcat() function with those parameters, do this:

strcat(xrray_ptr, brray_ptr);

... which won't work because the variables named xrray_ptr and brray_ptr do not exist.

i have read the links and looked at the examples. they dont seem to get through to me...

Then read them again, and again, and again, until they do.
http://cplusplus.com/doc/tutorial/functions/
Last edited on
The concatenation function must have a prototype before the main function or you'll have to define everything before the main functon.

Aceix.
i want to thank everyone who has been guiding me through this learning curve. i think i am getting closer to my target. please take a look at my code below and point out what im not doing right still. it seems i cant call the function.
thank you again!!

please see updated code below

Last edited on
1
2
3
4
5
cout << "This is the two strings writen together" <<endl;
char* str_cat (char p1in, char p2in); // this is not how you call a function!
// "to call" a function means using it, and feeding it data (in your case p1in and p2in)

str_cat(p1in, p2in); // this is how you call your function for p1in and p2in 


This below is not what you want to do.
1
2
3
4
char str_cat (char p1in, char p2in)
{
	return p1in + p2in;
}


You use char instead of char* or char[].

char can only hold one single letter, such as:
char a = 'Z';

char[] and char* can hold multiple letters (this isn't technically true for char* but it's a good enough approximation for now):
char message[] = "Hello!";

So you pretty much have to use either char[] or char* instead of char. You should go with char* but I'll leave it to someone else to explain why, if you really care.

1
2
3
char * str_cat(char *p1in, const char *p2in)
{
}


The const qualifier found at const char *p2in signifies that we don't intend to modify p2in.
We do intend to modify p1in, because that's how these functions work.

By the way, instead of p1in and p2in I suggest you use the names dest (for "destination") and src (for "source").

1
2
3
char * str_cat(char *dest, const char *src)
{
}


Alright, now how to append src to dest?
Well, we first need to go to dest's end, and that's where we start copying src's data until we reach src's end.

How can we find src's and dest's ends without knowing their size? Well it's easy: they both are NUL-terminated strings. NUL-terminated means they have a special character at the end with the value of zero.

1
2
3
4
5
6
7
8
9
char * str_cat(char *dest, const char *src)
{
    while (*dest != '\0')
        ++dest;

    // at this point dest is set at the position of its NUL character
    // "Hello!0"
    //        ^
}


And this is where I stop, for now. If you do not understand the code above, you need to read about strings, and pointers.
http://cplusplus.com/doc/tutorial/ntcs/
http://cplusplus.com/doc/tutorial/pointers/
Last edited on
good morning, i was back at this problem and with your help fixed it up to the point that it runs. but i am still fighting with reading the function out into the main. i am thinking i have an incomplete function?!

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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>

using namespace std;

char* str_cat (char *dest, const char *src)
{
	while (*dest != '\0')
		++dest;
	
	return dest;
}


int main()
{
	char xrray [] = "To be or not to be, that is the question... .";
	cout << "1st array reads: \n " << xrray << endl << endl;

	char brray [] = "Tomorrow and tomorrow creeps in this petty place... .";
	cout << "2nd array reads: \n " << brray << endl << endl;

	char* xrray_pointer = xrray;
	char* brray_pointer = brray;
	
	str_cat (xrray_pointer, brray_pointer);
	
	cout << "\n The Function will concatenate the two strings and display it here:" << endl;

	cout << xrray_pointer << endl;

system ("pause");
return 0;
}
i am thinking i have an incomplete function?!


The function str_cat() provided by me is incomplete.

Catfish3 wrote:
Alright, now how to append src to dest?
Well, we first need to go to dest's end, and that's where we start copying src's data until we reach src's end.

It completes the first step, of finding the end of dest.
After this you need to add a similar loop, to copy src into dest:
1
2
3
4
while (*src != '\0')
{
    // ...
}


If you need more help, you'll receive it, but this is very simple and I'm sure you can do it on your own if you try.
Last edited on
Dear all,

after feeling frustrated from not being able to finish this code, WITH YOUR HELP, i was able to come through. However, i am not out of the woods yet. Please see the code below and let me know what to do about this error message i get at run time:

Run-Time Check Failure #2 - Stack around the variable 'xrray' was corrupted.


the code is as follows:
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
/*
1.	Declares and initializes a character string to read as follows:
	char xrray[80] = “To be or not to be, that is the question…..”
	char brray[ ] = “Tomorrow and tomorrow creeps in this petty place …..”
2.	Contains a function, char* str_cat(char *xrray_ptr, char *bxrray_ptr), which concatenates the second string onto the end of the first string. 
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>

using namespace std;
 
char* strcat (char *dest, char *src)
{
	while ( *dest != '\0')
	++dest;
	{
		while ( *src != '\0')
			dest = dest + *src;
			cout << endl;
	}
	
return 0;
}


int main()
{
		char xrray [80] = "To be or not to be, that is the question... .";
		cout << "1st xrray reads: \n " << xrray << endl << endl;

		char brray [] = "Tomorrow and tomorrow creeps in this petty place... .";
		cout << "2nd xrray reads: \n " << brray << endl << endl;

		char* pxrray = xrray;
		char const* pbrray = brray;
	
			cout << "\n The 'strcat' function will concatenate the two strings and display it here:" << endl;
			strcat (pxrray, pbrray);
			cout << pxrray << endl;

	system ("pause");
	return 0;
}
Dear Catfish3, special thanks to you for your advice and guidance!
Your progress is good, but you made a couple of mistakes which I'll talk about.

1) I renamed the function to str_cat() to avoid confusion with C++'s ready-made std::strcat() -- which in turn becomes strcat() because of your using namespace std; directive.

2) char xrray [80] I'm pretty sure that's not enough space, and after having changed it to char xrray[500] it seems to work.

3) Why use pxrray and pbrray? Arrays decay to pointers. That's why you can assign an array to a pointer in the first place. This means: you can just pass directly the arrays:

str_cat(xrray, brray);

4) The str_cat() function itself isn't correct. In the second loop, you must assign the current value of *src to *dest, then increase both.

If point 4) is confusing, think of it like this: dest and src are basically local variables in the str_cat() function, and their original values are the memory addresses of the first elements in the arrays xrray and brray.

If you add to a pointer, you change its content (its content being a memory address, which is a number). Only when you use the dereference operation (asterisk *) will you go to that memory address and change what's stored there.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstddef>
#include <iostream>

int main()
{
    int ia[] = {0, 1, 2, 3, 4, 5}; // int array
    int *pi = ia; // pointer to int

    pi += 3;
    *pi = 777;

    for (std::size_t i=0; i < sizeof ia / sizeof *ia; ++i)
        std::clog << "ia[" << i << "] == " << ia[i] << '\n';
}
ia[0] == 0
ia[1] == 1
ia[2] == 2
ia[3] == 777
ia[4] == 4
ia[5] == 5


And here is the program. Please be consistent in your indentation, your original code looks spilled all over the place.

Also, str_cat() is currently a void function. I don't know if you need to change that or not.

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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>

using namespace std;

void str_cat(char *dest, char *src)
{
    while (*dest != '\0')
        ++dest;

    while (*src != '\0')
    {
        *dest = *src;
        ++dest;
        ++src;
    }
}

int main()
{
    char xrray[500] = "To be or not to be, that is the question... .";
    char brray[] = "Tomorrow and tomorrow creeps in this petty place... .";

    cout << "1st xrray reads: \n " << xrray << endl << endl;
    cout << "2nd xrray reads: \n " << brray << endl << endl;

    // there's no need for these pointers
    //char* pxrray = xrray;
    //char const* pbrray = brray;

    cout << "\n The 'strcat' function will concatenate the two strings and display it here:" << endl;
    str_cat(xrray, brray);
    cout << xrray << endl;
    system("pause");
    return 0;
}


Edit: I noticed there's a bug in my version of str_cat().

*dest = '\0'; should be added after the second loop.

This is to ensure that dest will be NUL-terminated.
Last edited on
Dear Catfish3, thank you for everything!

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
/*
1.	Declares and initializes a character string to read as follows:
	char xrray[80] = “To be or not to be, that is the question…..”
	char brray[ ] = “Tomorrow and tomorrow creeps in this petty place …..”
2.	Contains a function, char* str_cat(char *xrray_ptr, char *bxrray_ptr), 
        which concatenates the second string onto the end of the first string. 
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>

using namespace std;

void str_cat(char *dest, char *src)
{
    while (*dest != '\0')
        ++dest;

    while (*src != '\0')
    {
        *dest = *src;
        ++dest;
        ++src;
		*dest = '\0';
    }
}

int main()
{
    char xrray[200] = "To be or not to be, that is the question... .";
    char brray[] = "Tomorrow and tomorrow creeps in this petty place... .";

    cout << "1st xrray reads: \n " << xrray << endl << endl;
    cout << "2nd xrray reads: \n " << brray << endl << endl;

    cout << "\n The 'strcat' function will concatenate the two strings and display it here:" << endl;
    str_cat(xrray, brray);
    cout << xrray << endl;
    system("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.