# strcat

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm having a problem with the strcat function. I'm new to programming and just doing some simple exercises. Here's my problem, i ask the user to enter their first name, and surname, these are then stored in too seperate arrays. These arrays are then concatenated(sp?) using the strcat function and then copied into a final string which is then output to the screen. The only problem being that their is no space between the first and second name. Is it possible to ensure that a space goes in there? Thanks in advance. Here's the code: #include <iostream.h> #include <string.h> const int ONE_NAME_SIZE = 15; const int FULL_NAME_SIZE = 30; void main() { char FirstName[ONE_NAME_SIZE]; char Surname[ONE_NAME_SIZE]; char FullName[FULL_NAME_SIZE]; cout << "Enter your first name: "; cin >> FirstName; cout << "Enter your surname: "; cin >> Surname; strcat(FirstName,Surname); strcpy(FullName,FirstName); cout << FullName; system("pause"); }

##### Share on other sites
strcat() doesn't know anything about what 'should' go between the first and second strings; if you want a space there, you'll have to insert it manually (e.g. by making an additional call to strcat() with " " as the second argument).

On another note, are you sure you're not writing past the ends of any of your arrays? Are you sure you'll never write past the ends of any of your arrays? In any case, it's usually advisable to use the *n* versions of the C string manipulation functions, such as strncat() (if available - I can't remember if these are standard or not).

Also, I'm assuming you have some reason for doing these exercises using C string manipulation techniques, but just for comparison, here's an equivalent program in (idiomatic) C++:

#include <iostream>#include <string>using std::cout;using std::cin;int main(){    cout << "Enter your first name: ";    std::string firstName;    cin >> firstName;    cout << "Enter your surname: ";    std::string surName;    cin >> surname;    std::string fullName = firstName + " " + surname;    cout << fullName;}

##### Share on other sites
I'll spare you the "use std::string" thing.

you could do, for example:

sprintf(FullName, "%s %s", FirstName, Surname);

(Edit: beaten)

##### Share on other sites
Thanks alot for your quick replies. I understand the function much better now and can get this exercise done.

Thanks again for the help.

##### Share on other sites
Hello.

strcat is a primarily a C function. Though it can be used in C++, there are superior alternatives.

In C++ we use variables of type std::string to store and manipulate text. We can find this type in the header file <string> (note: no .h extension). In fact all of the Standard C++ Library headers do not have an extension. So we would use <iostream>. All of the contents of the Standard C++ Library lives in namespace std. If you are only starting C++ you wil not be familiar with namespaces so this probably means nothing to you. For now, all that this means is that to use something from the standard library called name you must either write "std::name", or write "using std::name" towards the beginning of your source file (just after the #include directives)*.

std::string uses "+" to concatenate either strings or characters together.

Your code rewritten to use std::string:
#include <iostream>#include <string>// needed for std::system#include <cstdlib>using std::string;using std::cout;using std::cin;int main(){   string FirstName;   string Surname;   cout << "Enter your first name: ";   cin >> FirstName;   cout << "Enter your surname: ";   cin >> Surname;   string FullName = FirstName + ' ' + Surname;   cout << FullName;   std::system("pause");}

* you can write it other places eventually. however this simplistic view will suffice for now.

Now consider the same written using strcat. All you need to do is strcat the string " " in between FirstName and SurName.

Note that your logic is potentially flawed, you first copy the surname to the fullname which is only 15 characters long( due to the fact that c-style strings must hold a special NUL character '\0' at the end, that is effectively only 14 characters). Then you copy the result into Fullname. It would be better to strcpy firstname into fullname, then strcat " " onto fullname and finally to strcat Surname onto fullname. You would have to increase FULL_NAME_SIZE to include room for the additional space character Actually no you wouldn't, as the 2 strings are both of size 14, plus the space makes for 29 characters which still leaves room for the NUL. I hope this demonstrates to you why we use std::string instead of raw character arrays, they handle this for us.

 note to self: Check for replies when returning from the bathroom to finish a post...

##### Share on other sites
Quote:
 Original post by jykAlso, I'm assuming you have some reason for doing these exercises in C, but just for comparison, here's an equivalent program in C++ (note that manipulation of strings is much more straightforward in C++):

I think the O.P wants to use C++ judging by the (deprecated) iostream.h header.

Ronnie TRFC: The book or tutorial you are learning from appears to be about a decade out of date. I'd recommend an upgrade.

The pedant in me also has to point out that main() must have a return type of int, not void or anything else.

##### Share on other sites
Quote:
Original post by the_edd
Quote:
 Original post by jykAlso, I'm assuming you have some reason for doing these exercises in C, but just for comparison, here's an equivalent program in C++ (note that manipulation of strings is much more straightforward in C++):
I edited that right after I posted, but you somehow managed to quote me first - drat! :)

##### Share on other sites
After reading what you have said Rip-Off i am beginning to wonder if the exercise question i am doing is possible. The question i am answering is question 5 after i have already completed 4. Is it possible to have 3 parameters in the strcat function in order for me to add the space? As the question insists that i must use strcat and strcpy in the answer.

Thanks again for your help mate.

The questions are below.

4) Write a program which asks the user for their first name. Store the name in an array. Then ask for their surname. The program should declare a third array large enough to hold both names including a space between them. The names should both be copied to the third array (with a space between them) and the resultant array output to the screen. Do not use any string library functions; manage the arrays yourself using loops to do the name copying and concatenation. Use fixed sized arrays and don’t worry about input range checks.

5) Modify your program for question 4) to use the standard ‘C’ library functions strcpy() and strcat(). You will need to include string.h in your program to use these functions

##### Share on other sites
Quote:
 Original post by Ronnie TRFCAfter reading what you have said Rip-Off i am beginning to wonder if the exercise question i am doing is possible.

It is possible [smile]

Quote:
 Is it possible to have 3 parameters in the strcat function in order for me to add the space? As the question insists that i must use strcat and strcpy in the answer.

Yes, but it does not state how many times you must use each.

##### Share on other sites
Quote:
 Original post by Ronnie TRFC5) Modify your program for question 4) to use the standard ‘C’ library functions strcpy() and strcat(). You will need to include string.h in your program to use these functions

You original program is almost OK then:

strcpy(FullName, FirstName);strcat(FullName, " ");strcat(FullName, Surname);

##### Share on other sites
Thank you Janta that's exactly what i was trying to get at, i just never thought of using strcat twice.

Cheers mate.

##### Share on other sites
Quote:
 Original post by Ronnie TRFCThank you Janta that's exactly what i was trying to get at, i just never thought of using strcat twice.Cheers mate.

That was just the code that rip-off suggested a couples posts before...You haven't read carefully enough [smile]

##### Share on other sites
Quote:
Original post by janta
Quote:
 Original post by Ronnie TRFCThank you Janta that's exactly what i was trying to get at, i just never thought of using strcat twice.Cheers mate.

That was just the code that rip-off suggested a couples posts before...You haven't read carefully enough [smile]
It was also suggested in the first sentence of the first reply to the thread :)

##### Share on other sites
Quote:
 Original post by jykstrcat() doesn't know anything about what 'should' go between the first and second strings; if you want a space there, you'll have to insert it manually (e.g. by making an additional call to strcat() with " " as the second argument).

Right about the second strcat but I meant my "code" was just exactly the same as what rip-off suggested when he said:

Quote:
 It would be better to strcpy firstname into fullname, then strcat " " onto fullname and finally to strcat Surname onto fullname

Allright, we're being nitpicky here [smile]

Cheers

##### Share on other sites
Quote:
 Original post by Ronnie TRFCHi, I'm having a problem with the strcat function. I'm new to programming and just doing some simple exercises. Here's my problem, i ask the user to enter their first name, and surname, these are then stored in too seperate arrays. These arrays are then concatenated(sp?) using the strcat function and then copied into a final string which is then output to the screen. The only problem being that their is no space between the first and second name. Is it possible to ensure that a space goes in there?

1) For Beginners.

2) What language do you think that code is in? Seriously. Throw that book away. Burn it, ideally. It's an abomination.

##### Share on other sites

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628641
• Total Posts
2983981

• 10
• 18
• 20
• 13
• 9