I'm trying to write a program that displays the longest common prefix using the divide and conquer method. My code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char *lcpUtil(char str1[], char str2[])
{
    char *result = NULL;
    result = (char *)malloc(sizeof(char) * 100);
    int n1 = strlen(str1), n2 = strlen(str2);
    
    for(int i = 0, j = 0; i <= n1-1 && j <= n2-1; i++, j++)
    {
        if(str1[i] != str2[j])
            break;
        strncat(result, &str1[i], 1); // append the prefix to the result string
    } 
    
    return (result);
}
const char *lcp(char **str, int l, int r)
{
    char str1[100], str2[100];
    if(l == r)
    {
        return (str[l]);
    }
    if (l < r)
    {
        int m = (l + r)/2;
        
        strcpy(str1, lcp(str, l, r));
        strcpy(str2, lcp(str, m+1, r));
    }
    return (lcpUtil(str1, str2));
}
int main(int argc, char **argv)
{
    char *arr[4] = {"apple", "application", "april", "apartment"}; // ap
    int n = 4;
    char prefix[100];
    strcpy(prefix, lcp(arr, 0 , n-1));
    if(strlen(prefix))
    {
        printf("The longest common prefix: %s\n", prefix);
    }
    else
        printf("There is no common prefix");
    return 0;
}
If I run this, a get a segmentation fault, debugging with gdb says:
Program received signal SIGSEGV, Segmentation fault.
0x00005555555552b7 in lcp (str=<error reading variable: Cannot access memory at address 0x7fffff7fefa8>, l=<error reading variable: Cannot access memory at address 0x7fffff7fefa4>, 
    r=<error reading variable: Cannot access memory at address 0x7fffff7fefa0>) at lab11.c:23
#1  0x0000555555555325 in lcp (str=0x7fffffffde40, l=0, r=3) at lab11.c:30
I know I didn't free the memory allocated with malloc, but it should still be working. I'm not sure why this is happening. Any ideas?
 
     
    