Merge two strings into a vector

2
/**
    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 result I've done so far is this, I thought - I'll concatenate a letter in the even position and another letter in the odd position but in logic it did not stay as I imagined it.

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

    
asked by anonymous 22.06.2017 / 20:00

6 answers

3

Some problems I noticed

  • The comparison s3[i]%2 == 0 is wrong. To know if the position of the resulting string is even or odd, do i % 2 == 0
  • s1[i] and s2[i] should not use i as index. i is incremented at each loop and represents the index of the resulting string. This means that you will skip a character from each string because of the parity check, generating a wrong result. Instead, keep individual indexes for the first and second string.
  • It is necessary to check if the index of the two strings does not exceed their respective sizes.
  • Considering the points I mentioned 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++];
        }
    }
    

    Example online here .

        
    22.06.2017 / 20:52
    2

    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
    
        
    22.06.2017 / 21:09
    1

    Oops! the error is in the indexes you are passing in arrays s1 and s2. With each loop a letter is left behind. To correct this you must assign a different index for each variable s1 and s2. Follow the corrected code. I hope I have 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;
        }
    
        
    22.06.2017 / 20:52
    1

    Observing the care that @Maniero quoted and your related links, as well as the comments from the @mercador , I have put a pointer-based answer.

    The idea is to navigate with the pointer itself until it reaches the point of the null character. While it is possible to navigate both pointers referring to strings s1 and s2 , I navigate in both. If I get to the null character of one of them, I end the double navigation loop and step through each one individually. Note that if s1 has reached its end, a back loop in s1 to null character will not satisfy the condition of while .

    void intercala(char *saida, char *s1, char *s2) {
      while (*s1 != '
    void intercala(char *saida, char *s1, char *s2) {
      while (*s1 != '%pre%' && *s2 != '%pre%') {
        *saida = *s1;
        saida++;
        *saida = *s2;
        saida++;
        s1++;
        s2++;
      }
      while (*s1 != '%pre%') {
        *saida = *s1;
        saida++;
        s1++;
      }
      while (*s2 != '%pre%') {
        *saida = *s2;
        saida++;
        s2++;
      }
    }
    
    ' && *s2 != '%pre%') { *saida = *s1; saida++; *saida = *s2; saida++; s1++; s2++; } while (*s1 != '%pre%') { *saida = *s1; saida++; s1++; } while (*s2 != '%pre%') { *saida = *s2; saida++; s2++; } }
        
    28.10.2017 / 02:19
    0

    I could not 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);
    }
    
        
    22.06.2017 / 20:53
    0

    I'm new to this, mine looks 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;
    }
    
        
    28.10.2017 / 01:10