Interleave two strings in a vector

/**
    5. Faça um programa que receba 2 strings (A e B) e 
    retorne uma terceira string (C) formada pelos caracteres de A e B intercalados. 
    Ex.: Se A='Quarta' e B='Segunda', a resposta deve ser 'QSueagrutnada'
**/

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

main(){
    char s1[30], s2[30], s3[60];
    int i=0, tamanho=0;

    printf("String 1: ");
    scanf(" %s", s1);

    printf("String 2: ");
    scanf(" %s", s2);

    tamanho = strlen(s1) + strlen(s2);

    for(i; i < tamanho; i++){
        if(s3[i]%2 == 0){
            s3[i] = s1[i];
        }else{
            s3[i] = s2[i];
        }
    }

    printf(" %s", s3);
}

The pro result I have done so far is this, I thought - I will concatenate a letter in the even position and another letter in the odd position but in logic it was not as I imagined

insert the description of the image here

I know that in C I learned about the function strcat(), but I do not know how to apply this example.

Author: Lacobus, 2017-06-22

6 answers

Some problems I noticed

  1. the comparison s3[i]%2 == 0 is wrong. To find out if the position of the resulting string is even or odd Do i % 2 == 0
  2. s1[i] and s2[i] should not use i as an index. i is incremented with each loop and represents the index of the resulting string. This means that you will skip a character from each string on account of the parity check, generating a wrong result. Instead, keep individual indexes for the first and second string.
  3. it is necessary to check that the index of the two strings do not exceed their respective sizes.

Considering the points I cited above, the changes would leave the code like this:

for (i; i < tamanho; i++) {
    if (i % 2 == 0 && indiceString1 < tamanhoString1) {
        s3[i] = s1[indiceString1++];
    } else if (indiceString2 >= tamanhoString2) {
        s3[i] = s1[indiceString1++];
    }
    else {
        s3[i] = s2[indiceString2++];
    }
}

Online example here .

 3
Author: mercador, 2017-06-22 19:07:53

How about a specific function for your case:

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


char * intercalar( char * s, const char * a, const char * b )
{
    int i = 0;
    int lena = strlen(a);
    int lenb = strlen(b);
    int len = lena + lenb;

    for( i = 0; i < lena; i++ )
        s[ i * 2 ] = a[i];

    for( i = 0; i < lenb; i++ )
        s[ 1 + i * 2 ] = b[i];

    s[ len ] = 0;

    return s;
}


int main( int argc, char ** argv )
{
    char * a = "Quarta";
    char * b = "Segunda";
    char saida[100] = {0};

    intercalar( saida, a, b );

    printf("a = %s\n", a );
    printf("b = %s\n", b );
    printf("saida = %s\n", saida );

    return 0;
}

Testing:

$ ./intercalar 
a = Quarta
b = Segunda
saida = QSueagrutnad
 2
Author: Lacobus, 2017-06-22 19:19:51

Opa! the error is in the indexes you are passing in the S1 and s2 arrays. With each loop a letter is left behind. To fix this you must assign a different index for each variable s1 and s2. Follow the corrected code. I hope I helped!

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

    main(){
      char s1[30], s2[30], s3[60];
      int i=0,j=0, k=0, tamanho=0;

      printf("String 1: ");
      scanf(" %s", s1);

      printf("String 2: ");
      scanf(" %s", s2);

      tamanho = strlen(s1) + strlen(s2);

      for(i, j, k; i < tamanho; i++){

        if(i%2 == 0){
          s3[i] = s1[j];
          j++;
        }else{
         s3[i] = s2[k];
          k++;
        }
      }
        printf ("%d\n", tamanho);

      printf(" %s", s3);
      return 0;
    }
 1
Author: Diego Soares, 2017-06-22 18:52:28

Observing the care that @Maniero Citedand its related links, and also the observations of @mercador's answer, I decided to put a pointer-based answer.

The idea is to navigate with the pointer itself until you reach the point of the null character. As long as it is possible to navigate in both the pointers referring to the strings s1 and s2, I navigate in both. If I get to the null character of one of them, I end the loop of the double navigation and move to navigate in each of them individually. Note that if s1 has reached its end, a subsequent loop in s1 up to the null character will not meet the condition of while.

void intercala(char *saida, char *s1, char *s2) {
  while (*s1 != '\0' && *s2 != '\0') {
    *saida = *s1;
    saida++;
    *saida = *s2;
    saida++;
    s1++;
    s2++;
  }
  while (*s1 != '\0') {
    *saida = *s1;
    saida++;
    s1++;
  }
  while (*s2 != '\0') {
    *saida = *s2;
    saida++;
    s2++;
  }
}
 1
Author: Jefferson Quesado, 2017-10-28 00:19:52

I couldn't test, but probably this way it works:

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

void main(){
    char s1[30], s2[30], s3[60];
    int i=0, tamanho=0, indexS1 = 0, indexS2 = 0;

    printf("String 1: ");
    scanf(" %s", s1);

    printf("String 2: ");
    scanf(" %s", s2);

    tamanho = strlen(s1) + strlen(s2);

    for(i; i < tamanho; i++){
    if ((i < strlen(s1)) && (i < strlen(s2))) {
        if((i % 2) == 0){
            s3[i] = s1[indexS1];
            indexS1++;
        }else{
            s3[i] = s2[indexS2];
            indexS2++;
        }
    } else if (i < strlen(s1)) {
        s3[i] = s1[indexS1];
        indexS1++;
    } else if (i < strlen(s2)) {
        s3[i] = s2[indexS2];
        indexS2++;
    }
}

    printf(" %s", s3);
}
 0
Author: Roberto de Campos, 2017-06-22 18:53:07

I'm new to this, mine looked like this:

#inlcude <stdio.h>

int main() {
    char string1[100] ,string2[100];
    int i;

    for(i=0 ; i<100 ; i++){
        string1[i]=0;
        string2[i]=0;
    }
    scanf("%s" ,string1);
    scanf("%s" ,string2);

    for(i=0 ; i<100 ; i++){
        if(string1[i]>=34 && string1[i]<127){
            printf("%c" ,string1[i]);
        }
        if(string2[i]>=34 && string2[i]<127){
            printf("%c" ,string2[i]);
        }

    }


  return 0;
}
 0
Author: Brian Natã, 2017-10-28 00:41:12