C program to concatenate strings does not execute correctly

Asked

Viewed 108 times

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 above code compiles correctly in DEV C++, does not present any error. It is a code to concatenate the string word1 and word2, but always when I run it, instead of forming the word pao with mortadella, it displays only the word p. I can’t see where the mistake is.

1 answer

1


Marco, the problem is for in his main. Declare it so:

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

When I call one for, prefer to use variável++, so it will only be incremented after the first iteration.

Browser other questions tagged

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