strcat

Started by
13 comments, last by Zahlman 16 years, 6 months ago
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"); }
Advertisement
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;}
I'll spare you the "use std::string" thing.

you could do, for example:

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


(Edit: beaten)
Thanks alot for your quick replies. I understand the function much better now and can get this exercise done.

Thanks again for the help.
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.

[edit] note to self: Check for replies when returning from the bathroom to finish a post...
Quote:Original post by jyk
Also, 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.

Quote:Original post by the_edd
Quote:Original post by jyk
Also, 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! :)
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
Quote:Original post by Ronnie TRFC
After 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.
Quote:Original post by Ronnie TRFC
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


You original program is almost OK then:

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

This topic is closed to new replies.

Advertisement