Concatenate and show on screen

Asked

Viewed 54 times

-2

I don’t know how to print the strcat on the screen, I don’t know how to concatenate to the position typed in the scanf_s.

int main() {

    char string1[10], string2[10];
    int RU[1];
    char NomeSobrenome;

    printf("digite o Nome\n");
    gets_s(string1);

    printf("Digite o segundo nome\n");
    gets_s(string2);

    printf("Digite o ultimo digito do RU");
    scanf_s("%d", &RU[1]);

    strncat(string1, string2,RU[1]);

    NomeSobrenome = strncat;

    printf("%c", NomeSobrenome);

}
  • What exactly is the question? What data do you enter and what should the result be? Or it’s not even compiling, if it’s not what error you don’t understand (put the message in this case)?

1 answer

0

strncat concatenates one string at the end of the other, not in the middle.

You can use strcpy to copy the string2 entire to the desired memory position, so just manipulate that position within the string1

printf("Digite o ultimo digito do RU\n");
scanf("%d", &i);

strcpy(&string1[i], string2);
printf("%s", string1);

Note that this method is not safe, because string1 stores only 10 characters, the ideal would be to create a string that can store as much string1 and string2

int main() {
    char string1[10], string2[10], string3[20];
    int i;

    printf("digite o Nome\n");
    scanf("%s9", string1);

    printf("Digite o segundo nome\n");
    scanf("%s9", string2);

    printf("Digite o ultimo digito do RU\n");
    scanf("%d", &i);

    strcpy(string3, string1);
    strcpy(&string3[i], string2);

    printf("%s", string3);
}
  • the idea was to type first name : Fabio, type second name: saints, then say the last digit: 3, last stay san(Fabio)tos

  • I imagined. Someone else asked the exact same question. See if that help you.

Browser other questions tagged

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