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 :)

Recommended Answers

All 4 Replies

I think you need to spend some time tracing through your algorithm. I'm not sure exactly what it is supposed to do, but I don't think it is doing what you want it to do...
(Given "HELLO", the list it builds is H, E, L, LL, ...)

In any case, your problems are with strncpy().
There is no guarantee that the result string (aux) will be null-terminated, and the way you are doing it, it definitely will not be null-terminated. Make sure to put a '\0' at the end of the string.

Hope this helps.

PS. I know some libraries (notably MS) typedef away *s from pointers, but that is really bad style. If you are using a variable, you should not have to find a typedef to know whether it is a pointer or not, and it makes reading the code harder. It took me a minute to verify that the last three lines of Insert() were doing what they were supposed to be doing...

Thank you,that helped!
After i added the '\0' where it was needed i spent some hours trying to figure what's still wrong, for now to see that it was a silly mistake on line 27, '!' wasn't welcome there :)

And also there are few things which you need to look back again, and try avoid using function like getch and gets function instead use getchar and fgets function which does the same job like what the former one does. It just the matter of standardization and portability.

And don't cast the return type of malloc, there is something called internal ,typecasting which will type cast the return type for you. Makes your work more easy and convenient.

ssharish

>there is something called internal ,typecasting which will type cast the return type for you
The conventional terminology is "implicit conversion" while casting is another conventional term for explicit conversion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.