cookiejar
asked on
Concatenate two strings Last and First Name
I have input coming in as first name and last name.
What I output to screen or file, I would like to format as follows:
lastName, firstName , for example Doe, John. Is there a way to do this in C.
Is there a string function that would allow me to do. Are there any examples of this being done?
What I output to screen or file, I would like to format as follows:
lastName, firstName , for example Doe, John. Is there a way to do this in C.
Is there a string function that would allow me to do. Are there any examples of this being done?
>> I would like to format as follows: lastName, firstName
Just be careful that your target has enough memory to allow for the concatenation; otherwise you can get crashes (which will be relatively easy to fix); or worse, you just overwrite memory of the object next to your string object. Worse because now you may not detect the problem immediately, and when you do, it may not be obvious that you overwrote memory beyond the bounds of your string array (or allocated memory).
Just be careful that your target has enough memory to allow for the concatenation; otherwise you can get crashes (which will be relatively easy to fix); or worse, you just overwrite memory of the object next to your string object. Worse because now you may not detect the problem immediately, and when you do, it may not be obvious that you overwrote memory beyond the bounds of your string array (or allocated memory).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Karrtik, what do I insert between the braces in this statement?
combined[101] ={} ;
combined[101] ={} ;
ASKER
When I use the strcat to concatenate the last and first name, I not sure as to how to add the comma to separate the lastName, firstName. For example, it look like this, DoeJohn.
How would I include the comma in the destination parameter? I am newbie at C.
How would I include the comma in the destination parameter? I am newbie at C.
You can just say :
char combined[101] ={0} ;
This initialize all the elements in the array to null.
char combined[101] ={0} ;
This initialize all the elements in the array to null.
Below is an example on how to use sprintf, inline comments provided for each line.
char* first = "Doe"; //Length = 3
char* last = "John";//Length = 4
char* combined = new char[strlen(first) + strlen(last) + 2];/* Combined length is length of first + length of last +1 so total length is 3+4+2 = 9, 2 at end because the char* are null terminated so 1 for comma and 1 for the null terminator*/
sprintf(combined,"%s,%s",first, last);
Similar example for strcat is below, I have added inline comments for each line to explain what it does.
char* first = "Doe"; //Length = 3
char* last = "John";// //Length = 4
const char* commasymbol = ",";//comma symbol, length = 1
const int combinedlength = strlen(first) + strlen(last) + strlen(commasymbol)+1;// Combined length is length of first + length of last +2 ( length of comma which is 1 + 1 character for Null terminator at the end)
char* combined = new char[combinedlength];//so total length is 3+4+2 = 9
memset ( combined, 0, combinedlength );//initialize all elements of the array to NULL (0)
strcat(combined,first);
strcat(combined,commasymbol);
strcat(combined,last);
to add to above comments:
you normally would use a sufficently big char buffer for concatenation rather than to "spare" some memory and use dynamic memory. the latter is more error-prone and you have to free the buffer after use. so the code without dynamic memory is like
'new' requires a c++ compiler while you asked for c. so in c it would be malloc/free and not new/delete to create and free dynamic buffers.
note, if you don't use a dynamic buffer you should make sure that you don't copy beyond buffer size by a check like the following:
Sara
char* combined = new char[combinedlength];
you normally would use a sufficently big char buffer for concatenation rather than to "spare" some memory and use dynamic memory. the latter is more error-prone and you have to free the buffer after use. so the code without dynamic memory is like
char szFullName[256] = { '\0' }; // use a big buffer. '\0' is a zero byte but 0 also is ok.
strcat(szFullName, "Doe");
strcat(szFullName, ", ");
strcat(szFullName, "John ");
'new' requires a c++ compiler while you asked for c. so in c it would be malloc/free and not new/delete to create and free dynamic buffers.
char * pszFullName = (char *)malloc(strlen(szLastName)+2+strlen(szFirstName)+1);
strcat(pszFullName, szLastName);
strcat(pszFullName, ", ");
strcat(pszFullName, szFirstName);
....
// free buffer after use
free(pszFullName);
// the pszFullName is invalid now
note, if you don't use a dynamic buffer you should make sure that you don't copy beyond buffer size by a check like the following:
void OutputFullName(const char * szLastName, const char * szFirstName)
{
char szFullName[256] = { '\0' }; // to be safe use a big buffer
if (strlen(szLastName) + 2 + strlen(szFirstName) < sizeof(szFullName))
{
strcat(szFullName, szLastName);
strcat(szFullName, ", ");
strcat(szFullName, szFirstName);
printf("%s\n", szFullName);
}
}
Sara
Thanks Sara for point of new and delete being a C++ functionality. It was a miss from my end.
Here is a pure C version of my code which I shared earlier.
1> sprintf version
Here is a pure C version of my code which I shared earlier.
1> sprintf version
char* first = "Doe"; //Length = 3
char* last = "John";// //Length = 4
const char* commasymbol = ",";//comma symbol
const int combinedlength = strlen(first) + strlen(last) + strlen(commasymbol)+1;// Combined length is length of first + length of last +2 ( length of comma which is 1 + 1 character for Null terminator at the end)
char* combined =(char *)malloc(combinedlength);
memset ( combined, 0, combinedlength );//initialize all elements of the array to NULL (0)
sprintf(combined,"%s,%s",first, last);
//Do your work
//Finally at the end free the memory
free(combined);
2> strcat version char* first = "Doe"; //Length = 3
char* last = "John";// //Length = 4
const char* commasymbol = ",";//comma symbol
const int combinedlength = strlen(first) + strlen(last) + strlen(commasymbol)+1;// Combined length is length of first + length of last +2 ( length of comma which is 1 + 1 character for Null terminator at the end)
char* combined =(char *)malloc(combinedlength);
memset ( combined, 0, combinedlength );//initialize all elements of the array to NULL (0)
strcat(combined,first);
strcat(combined,commasymbol);
strcat(combined,last);
//Do your work
//Finally at the end free the memory
free(combined);
This is something you can use to achieve the desired output.
The method is strcat.
http://www.tutorialspoint.com/c_standard_library/c_function_strcat.htm