Strings and modifyng them

Jan 16, 2012 at 12:50pm
I have the following exercise:

3. Write a program that contains four arrays. Three of the arrays should contain your
first name, middle initial, and last name. Use the string-copying function presented
in today’s lesson to copy these strings together into the fourth array, full name.

Problem is, I'm using "strcpy" but I can only copy one string in the other, and if I do it again, it overwrites the first one, I'm out of solutions, any ideas?
Last edited on Jan 16, 2012 at 12:51pm
Jan 16, 2012 at 12:55pm
You should use strncat instead of strcpy.
Jan 16, 2012 at 12:57pm
Thank you very much, I curse the day I bought this book, but I'll keep on trying to finish it since I started, forgot to ask though, if you have any other material that could help me, I would appreciate it a lot.
Jan 16, 2012 at 12:59pm
strcat(dest, src) is the same as strcpy(dest+strlen(dest), src), if you must.
Jan 16, 2012 at 1:01pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>

using namespace std;

main ()
{
     char cArray1 [] = "Jesse";
     char cArray2 [] = "Micheal";
     char cArray3 [] = "Leonard";
     char FullName [100];
     strcat(FullName, cArray3, cArray2, cArray1);
     cout << "This is my full name " << FullName <<endl;
     system("PAUSE");
     return 0;
}     

When I try it, I get an error and I'm sent to the string.h, what am I doing wrong?

The error is here, _CRTIMP char* __cdecl strcat (char*, const char*);, it says there are too many arguments, do I need to write the command in some other manner?
Last edited on Jan 16, 2012 at 1:02pm
Jan 16, 2012 at 1:01pm
Check out this tutorial:
http://www.learncpp.com

Last edited on Jan 16, 2012 at 1:04pm
Jan 16, 2012 at 1:04pm
Thanks Wisely Done, I will try to use it in tandem with my book, but do you have any idea why my code isn't functioning?
Jan 16, 2012 at 1:07pm
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>


using namespace std;

main ()
{
     char cArray1 [] = "Jesse ";
     char cArray2 [] = "Micheal ";
     char cArray3 [] = "Leonard ";
     char FullName [100];
     strcat(FullName, cArray1 );
     strcat(FullName, cArray2 );
     strcat(FullName, cArray3 );
     cout << "This is my full name " << FullName <<endl;
     system("PAUSE");
     return 0;
}
Jan 16, 2012 at 1:08pm
Nvm, got it, thanks guys, so it's the same as strcpy if just doesn't overwrite.

Edit: :)) right when I saw it, thanks Wisely Done.
Last edited on Jan 16, 2012 at 1:09pm
Jan 16, 2012 at 1:12pm
strcpy is usually used when copying to an empty string. While strcat appends one string to another. You should use strncat instead of strcat because strncat has a length check preventing buffer overflow.
Last edited on Jan 16, 2012 at 1:13pm
Topic archived. No new replies allowed.