Note that there are some explanatory texts on larger screens.

plurals
  1. POLongest Common Subsequence-Segmentation fault
    primarykey
    data
    text
    <p>I have to write a program to determine the longest common sub sequence.</p> <p>Input:</p> <p>The first argument will be a file that contains two strings per line, semicolon delimited. You can assume that there is only one unique subsequence per test case. e.g.</p> <p>XMJYAUZ;MZJAWXU</p> <p>Output:</p> <p>The longest common subsequence. Ensure that there are no trailing empty spaces on each line you print. e.g.</p> <p>MJAU</p> <p>I am using Dev C++ .. And it is compiling Fine!...But this question is a <a href="https://www.codeeval.com/public_sc/6/" rel="nofollow">programming challenge</a> and when i submit my answer it's showing me a segmentation fault!</p> <p>I have written the following code and i am getting a Segmentation Fault where am i wrong?</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; char str1[100],str2[100]; int len1; int len2; void printLCS(char b[len1][len2],char str1[],int i,int j) { if(i==0 || j==0) return; if(b[i][j]=='c') { printLCS(b,str1,i-1,j-1); printf("%c",str1[i-1]); } else if(b[i][j]=='l') printLCS(b,str1,i,j-1); else printLCS(b,str1,i-1,j); } void Seq(char str1[],char str2[]) { int i,j; len1=strlen(str1); len2=strlen(str2); int LCS[len1+1][len2+1]; char b[len1][len2]; for(i=0;i&lt;=len1;i++) { LCS[i][0]=0; } for(j=0;j&lt;=len2;j++) { LCS[0][j]=0; } for(i=1;i&lt;=len1;i++) { for(j=1;j&lt;=len2;j++) { if(str1[i-1]==str2[j-1]) { LCS[i][j]=1+LCS[i-1][j-1]; b[i][j]='c'; } else if(LCS[i-1][j]&gt;=LCS[i][j-1]) { LCS[i][j]=LCS[i-1][j]; b[i][j]='u'; } else { LCS[i][j]=LCS[i][j-1]; b[i][j]='l'; } } } printLCS(b,str1,len1,len2); } int main(int argc,char *argv[]) { if(argc!=2) { printf("Invalid Number of Arguments:\n"); exit(0); } FILE *fp; fp=fopen(argv[1],"r"); if(fp==NULL) { printf("File can't be opened:\n"); exit(0); } char c; c=fgetc(fp); while(c!=EOF) { int k=0; if(c=='\n') c=fgetc(fp); while(c!=';') { str1[k]=c; k++; c=fgetc(fp); } str1[k]='\0'; c=fgetc(fp); k=0; while(c!=EOF &amp;&amp; c!='\n') { str2[k]=c; k++; c=fgetc(fp); } str2[k]='\0'; Seq(str1,str2); printf("\n"); if(c==EOF) { break; } else c=fgetc(fp); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload