[Solved] Function that returns combination of two strings in C [closed]


Because is a summer saturday and I’m happy, I’ve written a little code for you, but it’s incomplete.

The only “prebuilt” function that the code uses is puts and I don’t see way to don’t use it.

The code has two problems:

  1. The strings cannot be longer than 64 bytes. If they are longer the result will be cutted.

  2. The strings should be of equal length. If are of different length the result will be cutted to the length of the shorter string.

Now you shall solve the two problems … I hope you do this!

You may run the code from the command line as

intesect string1 string2

… and it will give you the reply!

Here the code:

#include <stdio.h>

char * intersect(char *c, int clen, const char *a, const char *b);

int main(int argc, char *argv[])
{
    char c[129];

    if (argc<3) {
        puts("Usage: intersect stringA stringB\n");
        return 1;
    }

    puts(intersect(c,sizeof(c),argv[1],argv[2]));

    return 0;
}

char * intersect(char *c, int clen, const char *a, const char *b)
{
    int i=0,j=0;

    while (a[i] && b[i] && j<clen-2){

        c[j++]=a[i];
        c[j++]=b[i++];

    }

    c[j]=0;

    return c;

}

6

solved Function that returns combination of two strings in C [closed]