Problem with strings

Thread Solved

Join Date: Mar 2008
Posts: 17
Reputation: soultrav is an unknown quantity at this point 
Solved Threads: 0
soultrav soultrav is offline Offline
Newbie Poster

Problem with strings

 
0
  #1
Jul 11th, 2008
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.
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. typedef struct comp
  6. {
  7. char a[100];
  8. struct comp *next;
  9. } Comp,*TComp,**AComp;
  10.  
  11. void Insert(AComp term,char c[])
  12. {
  13. TComp aux=(TComp)malloc(sizeof(Comp));
  14. if(!aux) {printf("Memory allocation failed");return;}
  15. strcpy(aux->a,c);
  16. aux->next=NULL;
  17. while(*term)
  18. term=&(*term)->next;
  19. *term=aux;
  20. }
  21.  
  22. int Search(TComp term,char c[])
  23. {
  24. int length=strlen(c);
  25. while(term)
  26. {
  27. if(!strncmp(term->a,c,length)==0) return 1;
  28. term=term->next;
  29. }
  30. return 0;
  31. }
  32.  
  33. void Print(TComp term)
  34. {
  35. while(term)
  36. {
  37. printf("%s",term->a);
  38. term=term->next;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. TComp term=NULL;
  45. char c[100],aux[50],aux2[50];
  46. int i,j;
  47. printf("Type string:\n");
  48. gets(c);
  49. for(i=0;c[i]!='\0';i++)
  50. {
  51. strncpy(aux,c+i,1);
  52. if(!Search(term,aux))
  53. Insert(&term,aux);
  54. else
  55. {
  56. j=i+1;
  57. strncpy(aux,c+i,2);
  58. while(Search(term,aux))
  59. {
  60. j++;
  61. strncpy(aux,c+i,j-i+1);
  62. }
  63. strncpy(aux,c+i,j-i+1);
  64. Insert(&term,aux);
  65. i=j;
  66. }
  67. }
  68. Print(term);
  69. getch();
  70. return 0;
  71. }

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
Last edited by soultrav; Jul 11th, 2008 at 3:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Problem with strings

 
1
  #2
Jul 11th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: soultrav is an unknown quantity at this point 
Solved Threads: 0
soultrav soultrav is offline Offline
Newbie Poster

Re: Problem with strings

 
0
  #3
Jul 12th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Problem with strings

 
0
  #4
Jul 12th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 825
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with strings

 
1
  #5
Jul 12th, 2008
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 589 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC