Thread: joining strings with pointers?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    joining strings with pointers?

    I need my program to join two strings together. I want it to add the characters in source to the end of the string destination. The function assumes that there is enough room inside destination to store the additional characters. Can someone help me with this?

    Here is what i have so far, i just need to finish the function join:


    #include <stdio.h>


    void join (char* destination, char* source) {

    /* Need code here */

    }

    int main () {

    char quick_fox [100] = "The quick brown fox";
    char jumps_over [100] = " jumps over the lazy dog.";

    printf("Joining \"%s\" to \"%s\"\n", quick_fox, jumps_over);
    join(quick_fox, jumps_over);
    printf("gives \"%s\"\n", quick_fox);
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Why? When there's a perfectly good function in the std C library that does this - strcat()

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    The point of it is to not use that function.....

    Can you help?

  4. #4
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Code:
    void join (char* destination, char* source) {
       char *p1 = destination;
       char *p2 = source;
    
       while (*p1 != '\0') p1++; // find end of destination
       while (*p2 != '\0')            // loop till end of source
          *(p1++) = *(p2++);     // add char to end of destination
       *(++p1) = '\0';                // null terminate the destination
    }

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <stdio.h>
    
    void join (char* d, char* s) {while(*d)++d;while(*d++ = *s++);}
    
    int main () {
    
    	char quick_fox [100] = "The quick brown fox";
    	char jumps_over [100] = " jumps over the lazy dog.";
    
    	printf("Joining \"%s\" to \"%s\"\n", quick_fox, jumps_over);
    	join(quick_fox, jumps_over);
    	printf("gives \"%s\"\n", quick_fox);
    
    	return 0;
    }
    Its a crap implementation though......personally, if there's a std func to do something this simple, you would be mad not to use it.

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    yeah, i smell homework here
    hello, internet!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another method of cheating (yes, yes... I _guess_ we're supposed to use pointers...)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char quick_fox [100] = "The quick brown fox";
    	char jumps_over [100] = "jumps over the lazy dog.";
    	char joined[200];
    	
    	sprintf(joined, "%s %s", quick_fox, jumps_over);
    	
    	puts(joined);	
    
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM