Thread: Concatenation without string.h - program crashes

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by peripatein
    Wouldn't that prevent the procedure from accessing a location out of bounds?
    Yes, but is is also more difficult to understand than just checking for source[j] != '\0' or even using the concise loop that I showed you.

    Quote Originally Posted by peripatein
    Would you consider that to be a complete solution or is anything missing/unaccounted for?
    Looks fine, but of course, you should be printing outside of my_strcat.
    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

  2. #17
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Quote Originally Posted by laserlight View Post
    Yes, but is is also more difficult to understand than just checking for source[j] != '\0' or even using the concise loop that I showed you.


    Looks fine, but of course, you should be printing outside of my_strcat.
    Were you referring to my latest attempt, namely while (source[j] != '\0') destination[i++] = source[j++]; ? Or rather to the inversion of order?
    Moreover, is there no need to "manually" add '\0' to destination at the end? Is that taken care of by the loop?
    Last edited by peripatein; 05-13-2013 at 04:56 AM.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void my_strcat (char destination[], const char source[])
    {
        int i = 0, j = 0;
        while (destination[i] != '\0') i++;
        destination[i++] = ' ';
        while (source[j] != '\0') destination[i++] = source[j++];
        printf("%s\n", destination);
    }
    You forget to terminate "destination". Your loop ends when source[j] == '\0' thus you are not copying '\0' to "destination".

    Bye, Andreas

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    In that case, I could just stick to my original code, writing thus:
    Code:
    void my_strcat (char destination[], const char source[])
    {
            int i = 0, j = 0;
    	while (destination[i] != '\0') i++;
    	destination[i++] = ' ';
    	while ((j != 0 && source[j - 1] != '\0') || !j) destination[i++] = source[j++];
    }
    right?

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot do that source[j - 1] is invalid when j is 0.
    Kurt
    EDIT: I see it now, could work but it's a really strange solution
    Last edited by ZuK; 05-13-2013 at 06:05 AM.

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The main real goal in programming is to write maintainable code.

    You failed in that goal on you last post of code on this line.
    Code:
     while ((j != 0 && source[j - 1] != '\0') || !j) destination[i++] = source[j++];
    Much more complex than will be easy to maintain.

    Tim S.
    Last edited by stahta01; 05-13-2013 at 06:05 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #22
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    But it doesn't access that location once I include the condition j != 0 !! Why is that not maintainable?

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by peripatein View Post
    But it doesn't access that location once I include the condition j != 0 !! Why is that not maintainable?
    What functionality does the " || !j " add to your code?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #24
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by stahta01 View Post
    What functionality does the " || !j " add to your code?

    Tim S.
    Without that the loop wouldn't run at all. I said it "strange solution" just to save one line of code.
    Kurt
    Last edited by ZuK; 05-13-2013 at 06:12 AM.

  10. #25
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I personally find it more sophisticated. Yes, more cumbersome and complicated perhaps, strange you might add, yet this way the string (viz. destination) is added the enclosing '\0' by the while loop itself.

  11. #26
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I could have likewise written it thus:
    Code:
    while (!j || source[j - 1] != '\0') destination[i++] = source[j++];

  12. #27
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Don't write code to be unnecessarily clever; write code to be clear. Be considerate to the person who will maintain your code when you're gone. Even if you're just learning, get into this habit.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by peripatein
    Were you referring to my latest attempt, namely while (source[j] != '\0') destination[i++] = source[j++]; ? Or rather to the inversion of order?
    My comments correspond to what I quoted, i.e., the first comment for the first snippet; the second comment for the second snippet.

    Quote Originally Posted by peripatein
    Moreover, is there no need to "manually" add '\0' to destination at the end? Is that taken care of by the loop?
    You should add the null character at the end, and in fact I implied that in my earlier posts. My mistake on saying that your second code snippet is fine though, but in my defense I was too busy checking your first code snippet for correctness and did not notice that you missed that part out.

    Quote Originally Posted by peripatein
    I personally find it more sophisticated. Yes, more cumbersome and complicated perhaps, strange you might add, yet this way the string (viz. destination) is added the enclosing '\0' by the while loop itself.
    It is obfuscated, not sophisticated. Even the method that I showed you in post #14 is somewhat obfuscated, but at least it is well known. With it, you can simplify your function to:
    Code:
    void my_strcat(char destination[], const char source[])
    {
        int i = 0, j = 0;
        while (destination[i] != '\0') i++;
        destination[i++] = ' ';
        while (destination[i++] = source[j++]);
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. String concatenation
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2006, 07:19 PM
  3. K&R problem !! String concatenation :(
    By karanmitra in forum C Programming
    Replies: 9
    Last Post: 08-18-2005, 05:20 AM
  4. string concatenation
    By criticalerror in forum C++ Programming
    Replies: 11
    Last Post: 03-02-2004, 11:37 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM