arrays - LCS C Program Crashing on first input -
hello new c , having trouble getting code run, compiles fine when enter first input exe crashes. pretty sure problem lies in either memory allocation of array or use of pointers can not seem figure out issue.
here 2 files.
main.c
#include "stdheader.h" int main(int argc, char ** argv){ char* s1; char* s2; char* s3; while (scanf("%c\n", *s1) != (eof)){ scanf("%c\n", *s2); lcs(*s1,*s2,*s3); printf("%c \n", *s3); } }
lcs.c
#include "stdheader.h" void lcs(char*s1, char*s2, char*s3){ int i, j, k; int l1= strlen(s1); int l2= strlen(s2); int **a = (int**)malloc(sizeof(int *)*(l1+1)); (i=0; i<= l1; i++){ a[i]= malloc(sizeof(int)*(l2+1)); } (i=0; i<=l2; i++){ a[l1][i]=0; } (j=0; i<=l1; j++){ a[j][l2] = 0; } (i=l1-1; >= -1; i--){ (j=l2-1; j>= -1; j--){ if (s1[i]== s2[j]){ a[i][j]= 1 + a[i+1][j+1]; } else a[i][j] = fmax(a[i+1][j],a[i+1][j+1]); } } i=0; j=0; k=0; int true = 0; while(true=0){ if (i>l1 || j>l2 || a[i][j]==0) true=1; if (s1[i] == s2[j]){ s3[k]=s1[i]; i++; j++; k++; } else if (a[i][j+1] >= a[i+1][j]) j++; else i++; } for(i=0; i<= l1; i++){ free(a[i]); } free(a); s3[k]= '\0'; }
Comments
Post a Comment