Hello!
I am trying to make a program for string compression using LZ compression techniques, but the program is at the beggining - I fail to realise what goes wrong, but I think it is a problem with string operations.
#include <stdio.h>
#include <conio.h>
typedef struct comp
{
char a[100];
struct comp *next;
} Comp,*TComp,**AComp;
void Insert(AComp term,char c[])
{
TComp aux=(TComp)malloc(sizeof(Comp));
if(!aux) {printf("Memory allocation failed");return;}
strcpy(aux->a,c);
aux->next=NULL;
while(*term)
term=&(*term)->next;
*term=aux;
}
int Search(TComp term,char c[])
{
int length=strlen(c);
while(term)
{
if(!strncmp(term->a,c,length)==0) return 1;
term=term->next;
}
return 0;
}
void Print(TComp term)
{
while(term)
{
printf("%s",term->a);
term=term->next;
}
}
int main()
{
TComp term=NULL;
char c[100],aux[50],aux2[50];
int i,j;
printf("Type string:\n");
gets(c);
for(i=0;c[i]!='\0';i++)
{
strncpy(aux,c+i,1);
if(!Search(term,aux))
Insert(&term,aux);
else
{
j=i+1;
strncpy(aux,c+i,2);
while(Search(term,aux))
{
j++;
strncpy(aux,c+i,j-i+1);
}
strncpy(aux,c+i,j-i+1);
Insert(&term,aux);
i=j;
}
}
Print(term);
getch();
return 0;
}
The problem is that "Print(term)" should look exactly like "puts(c)", but information is lost down the way (it prints only the first letter of the string). And I also realised that the information in the variable "c" is lost down the way.
Any help would be appreciated :)