Thread: Merge Strings Alternatively with C

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    25

    Merge Strings Alternatively with C

    I'm working on a code that takes two strings and outputs one string with the following output:

    s1 = abc
    s2 = def
    output s3 = adbecf

    if string 1 has more than 3 characters, those characters are placed in the back. Same thing with string 2:

    s1 = abcwxy
    s2 = deflmn
    output s3 = adbecfwxylmn

    I'm pretty sure I have the answer but I keep getting an error in the compiler. What's going on? Can someone help me run it and tell me what error you get too please?

    Code:
    include<math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    
    
    /*
     * Complete the function below.
     */
    char* mergeStrings(char* a, char* b) {
        
        //char* arr;
        int alen = strlen(a);
        int blen = strlen(b);
        int len = alen + blen;
        char arr[len];
        int i = 0, j = 0, k = 0;
    
        while(i < alen && j < blen){
    
            arr[k++] = a[i++];
            arr[k++] = b[j++];
    
        }
    
        while(i < alen){
    
            arr[k++] = a[i++];
        }
    
        while(j < blen){
    
            arr[k++] = b[j++];
        }
    
        return arr;
    
    }
    int main() {
        FILE *f = fopen(getenv("OUTPUT_PATH"), "w");
        char* res;
        char* _a;
        _a = (char *)malloc(512000 * sizeof(char));
        scanf("\n%[^\n]",_a);
        
        char* _b;
        _b = (char *)malloc(512000 * sizeof(char));
        scanf("\n%[^\n]",_b);
        
        res = mergeStrings(_a, _b);
        fprintf(f, "%s\n", res);
        
        fclose(f);
        return 0;
    
    }
    new to C and C++

  2. #2
    Registered User
    Join Date
    Mar 2014
    Posts
    25
    It compiles successfully but I still get null as my answer

    Merge Strings Alternatively with C-capture-jpg
    new to C and C++

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a variable length array:
    Code:
    char arr[len];
    Now, the variable length array language feature was introduced in the 1999 edition of the C standard, but later became optional, so it might not be supported by your compiler... although it looks like your compiler, or at least the one involved in the automated testing, does support it. The thing is that this variable length array is local to the function, so this:
    Code:
    return arr;
    ends up returning a pointer to a (non-static) local variable, which is a Bad Thing. You could use dynamic memory allocation instead:
    Code:
    size_t alen = strlen(a);
    size_t blen = strlen(b);
    size_t len = alen + blen;
    char *arr = malloc(len + 1);
    I used size_t instead of int because that is the return type of strlen (and the type of the result of sizeof). You need the +1 to account for the terminating null character. You don't need to use sizeof(char) because that is always 1, and you don't need to cast the return value of malloc. Now, your function should work, except that in the caller (i.e., the main function), you should have a call to the free function to match the malloc in mergeStrings. You should also check that malloc does not return a null pointer.

    By the way, since you do not modify the source strings in mergeStrings, it should really be:
    Code:
    char* mergeStrings(const char* a, const char* b) {
    This way, the compiler would warn you if you do accidentally attempt to modify them, and so you can safely test with string literals, e.g., mergeStrings("abcwxy", "deflmn").

    Also, I recommend that you use descriptive names, and avoid coming up with names that begin with an underscore (in some situations, although admittedly not how you used them here, these may be reserved).
    Last edited by laserlight; 02-19-2019 at 09:35 PM.
    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

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    25
    It worked! Thank you so much!
    new to C and C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to merge two arrays
    By Pulock2009 in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2014, 09:24 AM
  2. Merge sort help
    By stfh in forum C Programming
    Replies: 3
    Last Post: 12-12-2012, 12:45 PM
  3. merge sort
    By moooon in forum C Programming
    Replies: 41
    Last Post: 01-01-2008, 08:12 PM
  4. Merge Sort
    By Echooo in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 03:16 PM
  5. sorting - merge -
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2006, 09:11 PM

Tags for this Thread