Thread: Concatenate two strings.

  1. #16
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    Quote Originally Posted by CommonTater View Post
    For example, you can write strlen in three lines...
    Code:
    int MyStrLen(char* str)
     { char* ptr = str;                         // don't want to disturb the input pointer.
        while(*ptr++);                           // find the null
        return (int) --ptr - str; }             // return the length
    .....

    So my advice would be... "Work in smaller blobs"....
    While I am sure the function you provided works as intended, I don't understand the logic behind it.

    I'm still working from a pretty basic knowledge pool and rather than just using an example I want to understand it prior to using it.

    Thanks for the advice, now I need to figure out exactly why that works.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    Alright so the refined and working function to find string length is completed. Overall resulting in a much more compact program.

    Code:
    /* concatenate two strings and malloc() memory to store a third strings */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 81
    
    
    char string1[MAX], string2[MAX];
    char* link_array(char first[], char second[]);
    int my_string_length(char string[]);
    	
    
    int main()
    {
        /* string input */
        puts("Enter your first line of text (maximum 80 characters): ");
        fgets(string1, sizeof(string1), stdin);
        
        puts("Enter your second line of text (maximum 80 characters): ");
        fgets(string2, sizeof(string2), stdin);
        
        printf("you entered %s", link_array(string1, string2));
        
        return 0;
    
    }
    
    char* link_array(char first[], char second[])
    {
        char *c; 
        long totalsize = 0;
        
        totalsize = totalsize + my_string_length(first) + my_string_length(second);
       
        c = malloc(totalsize + 1);
         
        if (c == NULL)
        {
            puts("Memory allocation failed");
            exit(1);
        }
         
        printf("You allocated %lu bytes of memory to store the string\n", totalsize + 1);
        
         /* combine the two strings using the memory addresses stored in end1 and end2 
         can't figure this part out */
         
        return c;
        
    }
    
    int my_string_length(char string[])
    {
        int ctr = 0;
        
        while(string[ctr] != '\0') {
             ctr++;
        }
        
        printf("length %d\n", ctr - 1);
        
        return ctr - 1;
    }
    Now comes the daunting part where I concatenate the two strings together.
    Last edited by ModeSix; 04-26-2011 at 09:09 AM.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ModeSix View Post
    While I am sure the function you provided works as intended, I don't understand the logic behind it.

    I'm still working from a pretty basic knowledge pool and rather than just using an example I want to understand it prior to using it.

    Thanks for the advice, now I need to figure out exactly why that works.
    You weren't supposed to take and use my examples... I was "pointing you in the right direction" not giving you free source code.

  4. #19
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    Quote Originally Posted by CommonTater View Post
    You weren't supposed to take and use my examples... I was "pointing you in the right direction" not giving you free source code.
    I wasn't taking your source code and using it, but I didn't understand the logic behind it. So I came up with my own version of it as you can see in my latest posting.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ModeSix View Post
    I wasn't taking your source code and using it, but I didn't understand the logic behind it. So I came up with my own version of it as you can see in my latest posting.
    Yep... saw that... in time you will figure out what I did... but by the time the compiler optimizes things, your way probably produces almost the same code as mine. You did with a counter, what I did with pointers...

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    So I have come up with the following method, since it's within the scope of what I have learned so far from my book. I am getting some strange output though.

    Code:
    /* concatenate two strings and malloc() memory to store a third strings */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 81
    
    
    char string1[MAX], string2[MAX];
    void link_array(char first[], char second[], long size);
    int my_string_length(char string[]);
    
    void concat(char c_string1[], char c_string2[], long ssize);
    
    long totalsize = 0;
    
    int main()
    {
    
        
        /* string input */
        puts("Enter your first line of text (maximum 80 characters): ");
        fgets(string1, sizeof(string1), stdin);
        
        puts("Enter your second line of text (maximum 80 characters): ");
        fgets(string2, sizeof(string2), stdin);
        
        totalsize = totalsize + my_string_length(string1) + my_string_length(string2);
        
        link_array(string1, string2, totalsize);
        
        concat(string1, string2, totalsize);
        
        return 0;
    
    }
    
    void link_array(char first[], char second[], long size)
    {
        char *c; 
        
        c = malloc(size + 1);
         
        if (c == NULL)
        {
            puts("Memory allocation failed");
            exit(1);
        }
         
        printf("You allocated %lu bytes of memory to store the string\n", size + 1);
          
        
    }
    
    void concat(char c_string1[], char c_string2[], long ssize)
    {
        char stringout[ssize + 1];
        int ctr = 0;
        int ctr2= 0;
        
        /* concatenate the two strings */
        
        while(c_string1[ctr] != '\0' && c_string1[ctr] != '\n')
        {
            stringout[ctr] = c_string1[ctr];
            ctr++;
        }
        
        /* insert a space between the strings */
        ctr++;
        stringout[ctr] = ' '; 
        
        while(c_string2[ctr2] != '\0' && c_string2[ctr2] != '\n')
        {
            stringout[ctr] = c_string2[ctr2];
            ctr++;
            ctr2++;
        }
        
        /* add null character to end of string */
        ctr++;
        stringout[ctr] = '\0';
        
        printf("%s\n", stringout);
        
    }
    
    int my_string_length(char string[])
    {
        int ctr = 0;
        
        while(string[ctr] != '\0') {
             ctr++;
        }
        
        printf("length %d\n", ctr - 1);
        
        return ctr - 1;
    }
    It seems to concatenate the strings together but not well, and I am getting some odd characters after the output as well as for spaces within the output.

    Code:
    Enter your first line of text (maximum 80 characters): 
    Testing with        
    Enter your second line of text (maximum 80 characters): 
    letters
    
    Actual output:
    length 13
    length 7
    You allocated 21 bytes of memory to store the string
    Testing with
    And if I put numbers in the input I get this:
    Code:
    Enter your first line of text (maximum 80 characters): 
    12345
    Enter your second line of text (maximum 80 characters): 
    67890
    
    Actual output:
    length 5
    length 5
    You allocated 11 bytes of memory to store the string
    1234567890??
    Any ideas why it sort of works?
    Last edited by ModeSix; 04-26-2011 at 09:55 AM.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) in link_array, you need to return the pointer to the allocated memory. It's not magic, you gotta tell it stuff...

    2) also in linke array there's no need to pass in the strings since you are calculating their lengths externally.

    3) in concat you are using a local array for your total strings... you should pass in the pointer to the memory allocated in link_array and use that. The local array will go out of focus as soon as the function exits and may be destroyed or overwritten, rendering it useless. You also need to return the pointer from concat, if for no other reason than you need to free() it before exiting the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenate
    By violatro in forum C Programming
    Replies: 16
    Last Post: 06-04-2010, 09:22 AM
  2. Replies: 2
    Last Post: 03-30-2009, 12:25 AM
  3. concatenate two strings! K&R problem
    By karanmitra in forum Linux Programming
    Replies: 2
    Last Post: 08-09-2005, 10:44 AM
  4. Concatenate chars and std::strings.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 08:20 AM
  5. concatenate
    By port in forum C Programming
    Replies: 1
    Last Post: 11-25-2002, 09:53 PM