Thread: Is there any way I can copy more than 1 string to another string?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    Is there any way I can copy more than 1 string to another string?

    Let's say I have

    char a[10], b[10], c[22];


    Is there anyway I can copy the content of a and b to c?

    I know how to copy one by one but I dont know how to copy more than 1 string.

    Maybe I can do strlen(a), and strlen(b) and then do something like

    for(n=0;n<strlen(a);n++){
    c[n]=a[n];}
    for(i=0;i<strlen(b),i++{
    c[i+strlen(a)]=b[i];
    c[strlen(a)+strlen(b)]='\0';}


    Edit: I use the above method with some modification and I got it worked. However, it is kinda long.
    is there any shorter way to do this?
    Last edited by byebyebyezzz; 06-27-2011 at 02:59 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Maybe std::string can help? You can copy the result into c if you want. Variables a and b can also still be arrays.

    Code:
    #include <string>
    
    std::string answer = a;
    answer += b;
    size_t len = answer.copy(c, sizeof(c) - 1);
    c[len] = '\0';

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    size_t len = answer.copy(c, sizeof(c) - 1);
    c[len] = '\0';
    This might be better accomplished by
    Code:
    if (sizeof(c) >= answer.size() + 1) // Make sure we don't get a buffer overrun!
    	strcpy(c, answer.c_str());
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or having gotten as far as using one std::string, why not use them for everything?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    thx for the help guys. I ended up using this

    std::string answer = a;
    answer += b;
    for(n=0;n<22;n++)
    c[n]=answer[n];
    c[n]='\0';

    I dont understand this
    size_t len = answer.copy(c, sizeof(c) - 1);
    c[len] = '\0';

    or this
    if (sizeof(c) >= answer.size() + 1) // Make sure we don't get a buffer overrun!
    strcpy(c, answer.c_str());

    they dont give me the right answer

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by byebyebyezzz
    thx for the help guys. I ended up using this

    std::string answer = a;
    answer += b;
    for(n=0;n<22;n++)
    c[n]=answer[n];
    c[n]='\0';
    This is wrong. You copy 22 characters from answer into c, but there is no guarantee that answer has 22 characters. Furthermore, you add the null terminator to c[22], but c only has 22 characters, so c[22] does not exist (you wrote beyond the boundary of the array).

    My question is the same as Salem's: having gotten as far as using one std::string, why not use them for everything?

    Granted, there are situations when you must resort to a null terminated string, but thus far there is no sign of that.

    Quote Originally Posted by byebyebyezzz
    I dont understand this
    size_t len = answer.copy(c, sizeof(c) - 1);
    c[len] = '\0';
    It copes at most sizeof(c) - 1, i.e., 21, characters from answer into c, then adds the null terminator.

    Quote Originally Posted by byebyebyezzz
    or this
    if (sizeof(c) >= answer.size() + 1) // Make sure we don't get a buffer overrun!
    strcpy(c, answer.c_str());
    Similiar idea: there is a check that buffer overflow will not happen, then the null terminated string version of answer is copied to c.

    Quote Originally Posted by byebyebyezzz
    they dont give me the right answer
    It is your other code that is wrong. Pick either of the two and post the smallest and simplest compilable program that demonstrates the problem. Also, state what exactly is the answer that you get, and what is the answer that you expect. If there is external input, state what input you provided.
    Last edited by laserlight; 06-28-2011 at 07:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by byebyebyezzz View Post
    thx for the help guys. I ended up using this

    std::string answer = a;
    answer += b;
    for(n=0;n<22;n++)
    c[n]=answer[n];
    c[n]='\0';
    There's an overflow there, as laserlight points out. i == 22 after the loop.

    I dont understand this
    size_t len = answer.copy(c, sizeof(c) - 1);
    c[len] = '\0';

    or this
    if (sizeof(c) >= answer.size() + 1) // Make sure we don't get a buffer overrun!
    strcpy(c, answer.c_str());

    they dont give me the right answer
    If those don't work but the first solution does, you have done something wrong or left something out of what you posted here. The 2nd one uses the std::string method "copy" into c, the return value of which is the number of bytes copied. That's where you want the null terminator, since the copy method does not add one. This one might not work correctly if c is a pointer and not an array.

    The 3rd one uses strcpy from <cstring>, which does append a null terminator. Either way, these should work. What is "wrong" about the answer you get?
    Last edited by MK27; 06-28-2011 at 07:21 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    You left some braces out of the "for" block, or else that will not work.
    No, that is not a problem since the null terminator only needs to be added once after the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    No, that is not a problem since the null terminator only needs to be added once after the loop.
    Check, I misread that and corrected my post.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    I ll post exactly what I have been doing. I already submit my assignment but I still want to learn how to improve my program.
    This is a frunction in one of my class.

    Code:
    void ISBN::toStrWithStyle(char* str) const{
    int n;
    char str1[14];
    
    std::string answer = area;   // use of std::string to copy everything in 1 string.
    answer += '-';
    answer += publisher;
    answer += '-';
    answer += title;
    answer += '-';
    answer += isbn[9];
    answer += '\0';
    for(n=0;n<14;n++){
    str1[n]=answer[n];}
    str1[n]='\0';
    if(registered==true){
    strcpy(str,str1);
    }
    else
    toStr(str);
    area, publisher, title and isbn are defined in class.
    If I use the other way, I get str = . or <.
    isbn=1111111111
    I want to get an out put that similar to this 11-1111-111-1
    The total length for str will be 13. I set my n=14.
    Last edited by byebyebyezzz; 06-28-2011 at 08:17 AM.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Once again, your writing beyond the last element in the array here: str1[n]='\0'; . Since you already put a null char at the end of answer, you can just remove this line.

    There is no need for str1; you can write to str directly. Use strcpy(str, answer.data() ).
    Last edited by King Mir; 06-29-2011 at 07:06 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But let us ask why you are using C-style strings? Is it a requirement? Are you allowed to only use std::string? If not, is there a reason why you don't go pure std::string and avoid this madness?

    Quote Originally Posted by King Mir View Post
    There is no need for str1; you can write to str directly. Use strcpy(str, area.data() ).
    AFAIK, data() is not guaranteed to null-terminate the string; hence this is dangerous. Use c_str() instead:
    strcpy(str, area.c_str());
    And don't forget to check for buffer overruns!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    But let us ask why you are using C-style strings? Is it a requirement? Are you allowed to only use std::string? If not, is there a reason why you don't go pure std::string and avoid this madness?


    AFAIK, data() is not guaranteed to null-terminate the string; hence this is dangerous. Use c_str() instead:
    strcpy(str, area.c_str());
    And don't forget to check for buffer overruns!
    Yes, but answer += '\0' is guaranteed to null terminate it.

    I don't know why I wrote area. It should be answer.
    Last edited by King Mir; 06-29-2011 at 07:06 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  2. Replies: 1
    Last Post: 12-10-2008, 11:29 AM
  3. copy a string to the end of another string
    By bartleby84 in forum C Programming
    Replies: 10
    Last Post: 04-04-2007, 05:32 AM
  4. copy a string in the end of another string!?
    By hyaku_ in forum C Programming
    Replies: 9
    Last Post: 11-29-2006, 06:22 AM
  5. Copy to string
    By cgod in forum C++ Programming
    Replies: 6
    Last Post: 11-20-2004, 08:30 AM