Copying strings in C [duplicate]

0

I'd like some help on the following question:

I have a char with a text of 32 characters, so I would like to know how to separate this char into two of 16 characters.

Type:

char textao[32] = "Oi meu nome e Cesar tudo bem ai?"

And it has to stay:

char textoInicio[16] = "Oi meu nome e Ce"
char textoFim[16] = "sar tudo bem ai?"

I used strncpy(textoInicio, textao, 16) for the first part and it worked. Now I do not know how to do it for the second time. I also did not want to use for .

Any tips?

    
asked by anonymous 25.05.2017 / 02:30

1 answer

0

First of all, in C you always need extra space in your array because strings need a null character (which can be represented with 0 or '&' ) to indicate where they end up. And to copy a string from a point, just use the %code% operator to return the memory address of the specific location you want to copy.

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

int main()
{
    char textao[33] = "Oi meu nome e Cesar tudo bem ai?";
    char textoInicio[17];
    char textoFim[17];

    /*copiando texto*/
    strncpy(textoInicio, textao, 16);
    textoInicio[16] = 0;

    strncpy(textoFim, &textao[16], 16);
    textoFim[16] = 0;

    printf("inicio: %s\n", textoInicio);
    printf("Fim: %s", textoFim);

    getchar();
    return 0;
}
    
25.05.2017 / 02:52