Longest Common Subsequence-Segmentation fault - c

I have to write a program to determine the longest common sub sequence.
Input:
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.
XMJYAUZ;MZJAWXU
Output:
The longest common subsequence. Ensure that there are no trailing empty spaces on each line you print. e.g.
MJAU
I am using Dev C++ .. And it is compiling Fine!...But this question is a programming challenge and when i submit my answer it's showing me a segmentation fault!
I have written the following code and i am getting a Segmentation Fault where am i wrong?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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<=len1;i++)
{
LCS[i][0]=0;
}
for(j=0;j<=len2;j++)
{
LCS[0][j]=0;
}
for(i=1;i<=len1;i++)
{
for(j=1;j<=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]>=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 && 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;
}

I dont know system of this site but;
i compiled with no error,
and result was true.
You didnt close file. Maybe memory leak etc. didnt allowed by site.
And, dont use global variables, unless you dont know another solution
this usage is very very bad! ISO C90 forbids this, anyway
int len1;
int len2;
void printLCS(char b[len1][len2]...
good luck.

If you've got access to a Mac or Linux system, there's a fantastic tool called valgrind which can help you track down these kinds of errors: basically it runs your program in a virtual machine and monitors what it reads and writes to memory.
Whilst I can't compile your code, I'm pretty suspicious about this for loop:
for(i=1;i<=len1;i++)
{
for(j=1;j<=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]>=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';
}
}
}
Arrays in C and C++ start at 0, so the maximum offset you're interested in is probably strlen - 1. Try changing your for loops to
for(i=1;i<len1;i++)
{
for(j=1;j<len2;j++)
{
...
}
}

Related

how to change the program to do the same task without the program crashing?

I have an assignment to produce a program that will compare students answer to the answer key, and display the incorrect answers. The program then produces a report of the students incorrect answers and his final grade. The Program must use arrays and functions.
Currently I am trying to code two functions one to read the students answer file and store it in an array and the other to read answer key file and store it in another array. Then the functions will return both arrays to the main function later to be sent to another function to compare their contents(not yet done).
My problem with this code after pressing F11 to compile and run, i get a blank execution screen and a notification saying that the program has stopped working.
If my code contains a mistake or my approach is incorrect please tell me how to fix it.
note: this is my first semester learning C programming.
Thank you.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
//Modules
char* readstudent()
{
FILE*s_ans;
int i,j;
static char arrs[20];
s_ans=fopen("trial2.txt","r");
if (s_ans == NULL)//check if file can be opened
{
printf("error student");
}
while(!feof(s_ans))
{
for(j=0;j<20;j++)
{
fscanf(s_ans,"%s",arrs[j]);
}
}
printf("ReadStudent\n");
for(i=0;i<20;i++)
{
printf("%d\t %s\n",i+1,arrs[i]);
}
return arrs;
}
char* readcorrect()
{
FILE*c_ans;
int x,i;
static char arrc[20];
c_ans=fopen("CorrectAnswers.txt","r");
if (c_ans == NULL)//check if file can be opened
{
printf("error correct");
}
while(!feof(c_ans))
{
for(x=0;x<20;x++)
{
fscanf(c_ans,"%s",arrc[x]);
}
}
printf("ReadCorrect\n");
for(i=0;i<20;i++)
{
printf("%d\t %s\n",i+1,arrc[i]);
}
return arrc;
}
//Main
int main()
{
int i,j,n,x;
char* as_ans=readstudent();
char* ac_ans=readcorrect();
printf("Main");
for(i=0;i<20;i++)
{
printf("%s",as_ans[i]);
}
return 0;
}

Code for ACODE on Spoj is not working

I have tried all possible test cases for this SPOJ question that I came across, but still my code is not getting accepted. Can't identify which test case it is failing.
I have considered the cases where zeroes can be inside the input. Also I have considered the cases of consecutive zeroes.
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,ar[6010];
char str[6010];
unsigned long long int dp[6010];
while(1)
{
int flag=0;
scanf("%s",str);
if(str[0]=='0')
break;
for(i=0;str[i]!='\0';i++) //copy string to array
{
ar[i+1]=str[i]-'0';
}
n=i;
for(i=1;i<=n-1;i++) //checking for continous two zeroes
{
if(ar[i]==0&&ar[i+1]==0) flag=1;
if(ar[i]>2&&ar[i+1]==0)flag=1;
}
dp[1]=1;
if(ar[1]*10+ar[2]<=26&&ar[2]!=0)dp[2]=2;
else dp[2]=1;
if(ar[2]==0)dp[1]=0;
for(i=3;i<=n;i++)
{
if(ar[i]!=0)
{
dp[i]=dp[i-1];
if(ar[i-1]*10+ar[i]<=26)
{
dp[i]+=dp[i-2];
}
}
else
{
if(ar[i-2]*10+ar[i-1]<=26)
{
dp[i]=dp[i-2];
dp[i-1]=0;
}
else
{
dp[i]=dp[i-1];
dp[i-1]=0;
}
}
}
if(flag==0)
printf("%llu\n",dp[n]);
else
printf("0\n");
}
return 0;
}

Allocation to Pointers

I work with char ****pCourses
int Courses(char ****pCourses, int ****pGrades, int **pCount, int *count)
{
char *buffer=NULL;
char letter;
int j=0, i;
int size=20;
int countCourse=0, sumCourses=0;
buffer=(char *)malloc(size*sizeof(char));
do
{
scanf("%c",&letter);
}while(letter==32);
while(letter!=10)
{
while(letter!=',')
{
//in case we need to expend the buffer
if(j>size)
{
size*=2;
buffer=(char *)realloc(buffer,size*sizeof(char));
}
buffer[j]=letter;
j++;
//The new letter from the name of course
scanf("%c",&letter);
}
//The end of the name of course
buffer[j]='\0';
j=0;
if(countCourse==0)
{
*pCount=(int *)realloc(*pCount, ((*count)+1)*sizeof(int));
(*pCount)[*count]=1;
}
else
(*pCount)[*count]++;
//Add the course's name to the system
*pCourses=(char ***)realloc(*pCourses, ((*count)+1)*sizeof(char ***));
(*pCourses)[*count]=(char **)realloc((*pCourses)[*count],(countCourse+1)*sizeof(char *));
((*pCourses)[*count])[countCourse]=(char *)malloc(strlen(buffer)+1);
strcpy(((*pCourses)[*count])[countCourse], buffer);
countCourse++;
scanf("%c",&letter);
}
free(buffer);
return 0;
}
while running I get a problem because of the next line:
(*pCourses)[*count]=(char **)realloc((*pCourses)[*count],(countCourse+1)*sizeof(char *));
that leads me to this part (from dbgheap.c):
{
while (nSize--)
{
if (*pb++ != bCheck)
{
return FALSE;
}
}
return TRUE;
}
does someone know what it means or why does this happen?
I think you've already been told what the problem is. All the alloc-type functions can fail and you should test for that and ideally write code that is robust enough to code with a failure.
Note that repeatedly using realloc() is not a good idea. If you expect to have to repeatedly add ( or remove ) data then you should be using a different structure, like a linked list or tree, which are designed to allow adding and removing data on the fly.

Why do i get this runtime error in this C programme? Please show me what's wrong

The task can be found here: http://www.talentbuddy.co/challenge/51846c184af0110af3822c32
And my programme regarding this task is the following:
#include <stdio.h>
#include<string.h>
void tokenize_query(char *query, char *punctuation) {
int i,j,ok=1,k,t;
char x[1000];
for(i=0;i<strlen(query);i++)
{
ok=1;
for(j=0;j<strlen(punctuation);j++)
{
if(query[i]==punctuation[j] || query[i]==' ')
ok=0;
}
if(ok!=0)
{
x[k]=query[i];
k++;
}
else {
for(t=0;t<k;t++)
{
printf("%c",x[t]);
}
k=0;
printf("\n");
}
}
}
k is uninitialised in the line
x[k]=query[i];
so you'll probably try to write beyond the end of the memory allocated for x.
The easiest fix is to initialise k when you declare it
int i,j,ok=1,k=0,t;
// ^^

please help me to find Bug in my Code (segmentation fault)

i am tring to solve this http://www.spoj.com/problems/LEXISORT/ question
it working fine in visual studio compiler and IDEone also but when i running in SPOJ compiler it is getting SEGSIGV error
Here my code goes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *getString();
void lexisort(char **str,int num);
void countsort(char **str,int i,int num);
int main()
{
int num_test;
int num_strings;
char **str;
int i,j;
scanf("%d",&num_test);
for(i=0;i<num_test;i++)
{
scanf("%d",&num_strings);
str=(char **)malloc(sizeof(char *)*num_strings);
for(j=0;j<num_strings;j++)
{
str[j]=(char *)malloc(sizeof(char)*11);
scanf("%s",str[j]);
}
lexisort(str,num_strings);
for(j=0;j<num_strings;j++)
{
printf("%s\n",str[j]);
free(str[j]);
}
free(str);
}
return 0;
}
void lexisort(char **str,int num)
{
int i;
for(i=9;i>=0;i--)
{
countsort(str,i,num);
}
}
void countsort(char **str,int i,int num)
{
int buff[52]={0,0},k,x;
char **temp=(char **)malloc(sizeof(char *)*num);
for(k=0;k<52;k++)
{
buff[k]=0;
}
for(k=0;k<num;k++)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
buff[(str[k][i]-'A')]++;
}
else
{
buff[26+(str[k][i]-'a')]++;
}
}
for(k=1;k<52;k++)
{
buff[k]=buff[k]+buff[k-1];
}
for(k=num-1;k>=0;k--)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
x=buff[(str[k][i]-'A')];
temp[x-1]=str[k];
buff[(str[k][i]-'A')]--;
}
else
{
x=buff[26+(str[k][i]-'a')];
temp[x-1]=str[k];
buff[26+(str[k][i]-'a')]--;
}
}
for(k=0;k<num;k++)
{
str[k]=temp[k];
}
free(temp);
}
Generally speaking, these online judge programs give an example input (in this case, that input appears to work perfectly), but also use a set of harder hidden inputs.
In this case, what happens if the input string has a space in it? For example, an input of:
1
2
hello orld
whateverss
In this case, your scanf("%s",str[j]); will not properly read that input line. I'd suggest switching over to a getline style interface, rather than a scanf style interface.

Resources