I'm having a problem and I can not figure out why. In main I create 2 arrays of strings. The function accepts 2 strings and creates a new array of the appropriate size according to the following requirement: For example: Array 1: aviv , Array 2: 12 , The new array: a12v12i12v The new array must be exactly the size! And then send the new array to main and main to print it. I also print junk values. I checked the size of the new array and it is the right size. My code:
 char* CreateString(char* str1, char* str2)
    {
        int length1, length2, length3 = 0;
        int i,j, index_help = 0;
        char *str3 = NULL;
        length1 = strlen(str1);
        length2 = strlen(str2);
        for (i = 0; i < length1; i++) // Check the size of the new array
        {
            length3++;
            if (i == (length1 - 1))
            {
                break;
            }
            for (j = 0; j < length2; j++)
            {
                length3++;
            }
        }
        str3 = (char*)malloc(length3+1  * sizeof(char));
        if (str3 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        for (i = 0; i < length1; i++) //Copying data
        {
            str3[index_help] = str1[i];
            if (i == (length1 - 1))
            {
                break;
            }
            for (j = 0; j < length2; j++) 
            {
                index_help++;
                str3[index_help] = str2[j];
            }
            index_help++;
        }
        return str3;
    }
    int main()
    {
        char *str1 = NULL, *str2 = NULL,*str4=NULL;
        int size1, size2,i;
        printf("enter the size of string number 1:\n");
        scanf("%d", &size1);
        printf("enter the size of string number2 :\n");
        scanf("%d", &size2);
        str1 = (char*)malloc((size1 + 1) * sizeof(char));
        if (str1 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        str2 = (char*)malloc((size2 + 1) * sizeof(char));
        if (str2 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        printf("Enter a value for the first string (the size is:%d):\n", size1);
        scanf("%s", str1);
        printf("Enter a value for the second string (the size is:%d):\n", size2);
        scanf("%s", str2);
        str4 = CreateString(str1, str2);
printf("%s",str4);
        printf("\n");
        return 0;
    }
 
    