-6

So if I input: "Ben" and "Tom", the returned string would be "BTeonm". I gotta do it without using prebuilt functions. I've got absolutely no idea how to approach this one, because I'm pretty new to C.

Any hint or help would be really appreciated!

EL02
  • 282
  • 2
  • 9
  • 2
    Caution: about returning a string from a function: [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c). – Weather Vane Aug 07 '21 at 16:52
  • 2
    You may create a function with a for loop that scans the two strings character by character and joins them into a third string! Pay attention to the string memory use! – Sir Jo Black Aug 07 '21 at 16:53
  • 1
    @WeatherVane yes, I have removed my comment. I didn't catch the question properly! Apologies. – mtm Aug 07 '21 at 16:58
  • 1
    So what have you tried so far? You should show an attempt to solve this on your own first. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Oka Aug 07 '21 at 17:17
  • @Oka The reply above gave me an idea. Pretty much i think i should create a third string and maybe treat it as an array? – EL02 Aug 07 '21 at 17:25
  • 5
    Nonetheless, simply asking how to do an assignment (or equivalent) is inappropriate for Stack Overflow, largely because it is too broad. Stack Overflow is for specific questions, such as one part of the problem. We do not know whether you know how to write a function definition in C, how to pass parameters, how to check the lengths of strings, how to allocate memory, how to write loops, how to access characters of strings, and so on. Stack Overflow is not a tutorial service. Look to primers for that. If you have a problem with one part of the task, ask about that specific part. – Eric Postpischil Aug 07 '21 at 17:28
  • 2
    And there are classes in August. Stack Overflow is used people people all over the world in all sorts of situations. – Eric Postpischil Aug 07 '21 at 17:29
  • @EricPostpischil well, I didnt even ask anyone to do the write the whole code, just for a tip or hint. But yeah other than that, I know how to pass them to functions, how to use pointers(to a certain extent), how to allocate memory with malloc() and ofc how to use loops. Just don't jump to conclusions with that gotcha line "Hint: That's an assignment". Really not cool. – EL02 Aug 07 '21 at 17:40
  • @GreasyLlama If you know all that, then you should really be able to do it yourself without help. Hint: how would you do this "by hand" with a pencil and a pice of paper? – Jabberwocky Aug 07 '21 at 17:48
  • @Jabberwocky i just know the basics of strings and cant seem to wrap my head around how to approach a problem without using the string.h library, like if you were to do that with int's in an array that would be pretty easy. I dont get the pencil and paper part. – EL02 Aug 07 '21 at 17:55
  • @GreasyLlama a string is basically an array of `char` with a final null character marking the end of the string. You don't need any of the functions from string.h do do what you're asking for. If you can do it with an array of `int` you can do it with an array of `char`. – Jabberwocky Aug 07 '21 at 18:19
  • @Jabberwocky Yes, but in c++ you can use the "+" operator in strings and in c you cannot, that's why I needed help. And yes, that's the point, nothing from string.h allowed. Anyway I think i get it now. Thanks everyone who helped. – EL02 Aug 07 '21 at 18:24
  • 1
    This is a fun little challenge. I'm going to play around with this. Unsure how far you have to take the "no prebuilt functions" rule, but taking that as a definite rule it poses a lot of interesting challenges. Cool question. – LEF Aug 07 '21 at 19:15

2 Answers2

1

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;

}
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22
  • Just a quick question, why is the c[j]=0; line needed? – EL02 Aug 07 '21 at 18:02
  • Because your previuos reply in the comments you should know this. The answer is that all C string are 0 terminated. If we don't insert the 0 at the end of the stirng all bytes till a byte of value 0 is reached will be printed. :) – Sir Jo Black Aug 07 '21 at 18:07
  • oh in c++ i use NULL in a "for" loop. Thanks anyway! – EL02 Aug 07 '21 at 18:09
  • It's better to use NULL for the pointers and not to indicate the value of 0. Anyway the C++ strings aren't C string! ... A C++ string is an object! It's better don't confuse, in C++, char[] with string. – Sir Jo Black Aug 07 '21 at 18:16
  • yeah thats what I was doing wrong. I thought i could add them together just by using "+". Now I get it that I should only assign them to a third char one by one. – EL02 Aug 07 '21 at 18:29
  • 1
    THIS is C ... not C++ and char[] aren't C++ strings! ;) – Sir Jo Black Aug 07 '21 at 18:33
1

Here is another way to approach it, without any string-length limitations:

char *strmix(const char *str1,const char *str2);

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

    if (argc<3) {
        puts("Usage: blend stringA stringB\n");
        return 1;
    }
    puts(p = strmix(argv[1],argv[2]));
    free(p);
    return 0;
}

char *strmix(const char *str1,const char *str2)
{
    char *str = malloc(strlen(str1) + strlen(str2) + 1);
    char *p = str;

    while (*str1  ||  *str2) {
        if (*str1)
            *p++ = *str1++;
        if (*str2)
            *p++ = *str2++;
    }
    *p = 0;
    return str;
}

If you are not allowed to use strlen, you should be able to create your own fairly easily.

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11