Computing.Net > Forums > Programming > c++ combine strings

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c++ combine strings

Reply to Message Icon

Name: Mechanix2Go
Date: December 13, 2005 at 03:05:49 Pacific
OS: w2k sp3
CPU/Ram: PIII 933/256MB
Comment:

I need the syntax to combine strings. This doesn't work:

//
char first[10]="hello ";
char last[20]="world";

char name[50]=first+last;
//

TIA



If at first you don't succeed, you're about average.

M2



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: December 13, 2005 at 09:38:13 Pacific
Reply:

combining char arrays requires that you use the C code function:
StrCat

Chi

They mostly come at night...mostly.


0

Response Number 2
Name: Mechanix2Go
Date: December 13, 2005 at 11:21:23 Pacific
Reply:

Hi Chi,

Thanks for that.

I got this syntax from the TC IDE help:

char *strncat(char *dest, const char *src, size_t maxlen);

But I have no idea how to write the line I need to combone my strings.

Help me out here.

TIA


If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: finitestateautomaton
Date: December 13, 2005 at 12:21:21 Pacific
Reply:

Hi! Mechanix2Go, it can be done as follows
1) using strcat()

strcat(name, first);
strcat(name, last);

2) using strncat()

strncat(name, first, strlen(first));
strncat(name, last, strlen(last));

string.h must be included. Hope this helps :)


0

Response Number 4
Name: Mechanix2Go
Date: December 13, 2005 at 17:57:08 Pacific
Reply:

Hi finite,

That got it.

Thank you very much.


If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: finitestateautomaton
Date: December 14, 2005 at 10:59:44 Pacific
Reply:

... and one more thing, if you want to overwrite any value assigned previously to the variable "name" then use "strcpy()" and "strncpy()" instead of "strcat()" and "strncat()" respectively on the first line ie the above code should be

1)

strcpy(name, first);
strcat(name, last);

2)

strncpy(name, first, strlen(first));
strncat(name, last, strlen(last));

the first line in each code overwrites the variable "name" and combines "first" and "last" to form the new value for "name".


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c++ combine strings

C Program: Finding End Of A String www.computing.net/answers/programming/c-program-finding-end-of-a-string/2716.html

C++ inputting strings as char array www.computing.net/answers/programming/c-inputting-strings-as-char-array/6305.html

C Char String Compairison www.computing.net/answers/programming/c-char-string-compairison/3401.html