Interlink two strings into one vector

Asked

Viewed 1,851 times

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 pro 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 didn’t look like I imagined

inserir a descrição da imagem aqui

I know in C I learned about function strcat(), but I don’t know how to apply in this example.

  • It is a pity that the code has the problems I was talking about in https://answall.com/a/213923/101. If I give a more idiomatic answer, even if not the one you want, just for reference to other people of a better code.

  • @bigown College addiction, they taught me this way and it’s how I’m charged there :d

  • That is, I’m discouraging you. People who don’t understand C teaching C.

  • @bigown Unfortunately..

6 answers

3


Some problems I noticed

  1. 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
  2. s1[i] and s2[i] shouldn’t use i as an index. i is incremented to each loop and represents the index of the resulting string. This means that you will skip a character of each string because of the parity check, generating a wrong result. Instead, keep individual indices for the first and second string.
  3. It is necessary to check that 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++];
    }
}

Online example here.

  • I must say, your probation has left me confused... but yes, it’s more than right

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
  • Lost the last character of the second. I believe your solution only works for strings of the same size, or in the case where len(a) = len(b)+1

1

Observing the care that the @Maniero quoted and its related links, as well as the observations of reply from @mercador, I decided to put a pointer-based response.

The idea is to navigate with the pointer itself until you reach the point of the null character. While it is possible to navigate both pointers referring to strings s1 and s2, sail in both. If I reach the null character of one of them, I finish the loop of the double navigation and I navigate in each of them individually. Note that, case s1 has reached its end, a later tie in s1 until the null character does not meet the condition of the 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

Oops! the error is in the indexes you are passing in arrays S1 and s2. At 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 to 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;
    }
  • I think your solution doesn’t fit very well when the size of the strings is very different. For example, sexta and domingo

0

I’m new at this, mine’s 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;
}
  • Your code has become deformed. Select the code and press Ctrl+k

  • 1

    Taking advantage, try to explain what you did, what your solution differs from the others.

0

I couldn’t test it, 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.