C program to concatenate strings does not execute correctly

2

include

int main(){

    void concatenarStrings(char string1[], int t1,
                           char string2[], int t2,
                           char string3[]);

    char palavra1[]={'p','a','o','c','o','m'};
    char palavra2[]={'m','o','r','t','a','d','e','l','a'};
    char novaPalavra[16];

    concatenarStrings(palavra1,6,palavra2,9,novaPalavra);

    int i;
    for(int i = 0;i < 1;++i){
        printf("%c",novaPalavra[i]);
    }


    return 0;
}

void concatenarStrings(char string1[], int t1,
                       char string2[], int t2,
                       char string3[]){
    int i,j;

    for(i = 0; i < t1; ++i){
        string3[i] = string1[i];
}
    for(j = 0; j < t2; ++j){
        string3[t1 + j] = string2[j];

    }                       
}

My code above compiles correctly in DEV C ++, it does not show any errors. It is a code to concatenate the strings word1 and word2, but always when I run it, instead of forming the word pao with mortadela, it displays only the word p. I can not see where the error is.

    
asked by anonymous 21.05.2017 / 23:59

1 answer

1

Marco, the problem is for in its main . Declare it like this:

//Você já declarou a variável i, então pode usá-la sem declará-la novamente
for(i = 0; i < 16; i++)

When calling a for , you would prefer to use variável++ , so it will only be incremented after the first iteration.

    
22.05.2017 / 00:25