LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-20-2004, 04:17 AM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
adding strings


how to add two string
string1="its"
string2="done"
how can i get "its done" in a single string/char
 
Old 08-20-2004, 04:35 AM   #2
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Rep: Reputation: 15
what are you programming in??
 
Old 08-20-2004, 05:27 AM   #3
pantera
Member
 
Registered: May 2004
Posts: 80

Original Poster
Rep: Reputation: 15
i am doing in C in linux
 
Old 08-20-2004, 05:35 AM   #4
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
If it is C/C++ you can do it by

#include <string.h>

strcat(string_one , string_two);

This funtion will concatinate string_one and string_two and store the resultant string in string_one.

If some other language you are using, then you can also use a loop to do that.
Make a string of size equal to size of string_one + string_two +1 and then copy both in that string with a loop char by char, Like

for (i=0; i<string_one.length();i++)
reult_string[i] = string_one[i];

for (i=string_one.length(); i<string_one.length()+string_two.length();i++)
reult_string[i] = string_two[i - string_one.length()];

reult_string[i+1] = '\0';


For the second case its just the logic for that, you can use the loops or arrays in the syntax of your own language.
 
Old 08-20-2004, 05:41 AM   #5
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Ok if you are doing in c++.

To copy a string you can use.
strcpy(destination , source);

To add two strings you can use.
strcat(destination , source);

In strcat you destination string must be of enough size to hold both the strings.

If string1 is 10 chars and string 2 is of 5 chars then you can declair a string 3 with 10+5+1=16 char.
One extra char for '\0' string terminater.

Then copy first string in string3 and then concatinate it with string2.

strcpy(string3 , string1);
strcat(string3 , string2);
 
Old 08-20-2004, 08:04 AM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,261

Rep: Reputation: 45
by cppkid
Ok if you are doing in c++.

this is not particularly relevant to pantera but in case anyone else reads this thread i would like to point out that you would be better using c++ strings rather than c strings in c++ because they have nice operators defined for them, the code in c++ would become

Code:
#include <string>

using std::string;

void somefunc() {
    string s1="its";
    string s2="done";

    s1+=(" "+s2);
}
if you do want to use c standard library header files in c++ then they should be prefixed with c and lose the .h, so #include <string.h> becomes #include <cstring>

finally for the c-string functions you should really use the n-versions as this will greatly help to reduce the chance of nasty things like buffer overflow.

Last edited by kev82; 08-20-2004 at 08:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
strings in c djgerbavore Programming 8 01-11-2005 04:27 PM
C....Fun with strings :( Scrag Programming 3 04-25-2004 02:12 PM
pointers and strings skibud2 Programming 4 09-22-2003 07:38 AM
adding strings in bash FireAge Linux - General 4 03-11-2003 10:57 AM


All times are GMT -5. The time now is 02:50 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration