0

I'm trying to write a program that accepts two string inputs in C that will then yield the result as the two strings interlaced.

An example of what I want:

input: abdc
input: efgh

result: aebfdgch

Here is the little bit of code that I have so far for that portion.

#include <stdio.h> 
#include <string.h>

#define maxLen 100

int main() {

   char string1[maxLen];
   char string2[maxLen];

   printf("please enter a string of up to 100 characters: ");
   fgets(shuffleString,maxLen,stdin);

   printf("please enter another string of up to 100 characters: ");
   fgets(shuffleString2,maxLen,stdin);

   return 0;
}

I have tried a lot of things to make the shuffling work, but nothing I seems to. This is for a homework assignment so I'm not trying to have someone write the code for me, but some examples and explanations would be nice. Also, if it's not already obvious, this is for an introuctory level C class so I would appreciate keeping things as close to my level as possible.

squiguy
  • 32,370
  • 6
  • 56
  • 63
  • what precisely do you mean by "yield the result" - print it, or...? – mfsiega Mar 06 '13 at 06:10
  • 3
    To interleave the two strings together, there is no existing function to do that. You will need to read them character by character and calculate some indexes. Does this sound like it's within what you're taught? – Nayuki Mar 06 '13 at 06:11
  • Try doing this on paper and look into how to use a loop. – aqua Mar 06 '13 at 06:12
  • 2
    Also, what are you supposed to do if one string is longer than the other? (e.g. What happens if you want to interleave abc with efghij?) – Nayuki Mar 06 '13 at 06:13

2 Answers2

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strlace(char *str1, char *str2)
{
    int i;
    int len = strlen(str1) + strlen(str2) + 1;
    char *str = (char*)malloc(len * sizeof(char));

    for (i = 0; i < strlen(str1); ++i) {
        str[i*2] = str1[i];
    }

    for (i = 0; i < strlen(str2); ++i) {
        str[1+i*2] = str2[i];
    }

    str[len - 1] = 0;

    return str;
}

int main()
{
    char *str1 = "abdc";
    char *str2 = "efgh";
    char *str3 = strlace(str1, str2);

    puts(str3);

    free(str3);

    return 0;
}

seems to work. Enjoy.

Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • 1
    this will stick string b after string a (i.e., concatenate a + b) rather than "interlace", as he asks – mfsiega Mar 06 '13 at 06:09
  • silly me. I misunderstood the question. The idea is similar, but requires loops rather than strcpy. – Dmytro Mar 06 '13 at 06:10
  • What will happen if the two input strings have different length? Also do not cast result from malloc: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858 and sizeof(char) is always equal to 1 by the definition. – emil Mar 06 '13 at 06:57
  • Actually, I think this code works flawlessly in any length... Show me a test case where it doesn't. – Dmytro Mar 06 '13 at 07:01
  • @Dmitry: Of course there is no change of behaviour in this case to cast the return value or usage of sizeof(char), but it is always better to write as simple code as possible to easier find bugs. So for demonstration purposes it is good to demonstrate not to use cast or sizeof(char). You should tell your instructors not to do sizeof(char). :) I am sorry if I have offended you. – emil Mar 06 '13 at 07:04
  • No problem. Its actually simpler for beginners to understand n * sizeof(char). People have a very hard time believing that char will always be 1 byte. A char was designed to store a character. With unicode out there, saying that char is always 1 byte is really confusing. – Dmytro Mar 06 '13 at 07:06
  • @Dmitry Hehe, that is true, unfortunately none of our implementations would handle UTF-8. – emil Mar 06 '13 at 07:15
0
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#define maxLen 100

char *
strinter(char *s1, char *s2)
{
    char *dst;
    int len = strlen(s1) + strlen(s2);

    dst = malloc(len + 1);
    if(dst == NULL)
        return NULL;
    for(;*s1 || *s2;){
      if(*s1)*dst++ = *s1++;
      if(*s2)*dst++ = *s2++;
    }
    *dst = '\0';
    return dst - len;
}

void
chomp(char *s)
{
    char *p = strchr(s, '\n');
    if(p != NULL)
        *p = '\0';
}

int
main(void)
{
    char string1[maxLen];
    char string2[maxLen];
    char *string3;

    printf("please enter a string of up to 100 characters: ");
    fgets(string1, maxLen, stdin);

    printf("please enter another string of up to 100 characters: ");
    fgets(string2, maxLen, stdin);

    /* remove newlines */
    chomp(string1);
    chomp(string2);

    string3 = strinter(string1, string2);
    if(string3 == NULL)
        return 1;
    printf("%s\n", string3);
    free(string3);
    return 0;
}

This will work even if the two input strings do not have the same length.

emil
  • 1,642
  • 13
  • 12