| | |
Problem with strings
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
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
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.
C Syntax (Toggle Plain Text)
#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
Last edited by soultrav; Jul 11th, 2008 at 3:58 pm.
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...
(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...
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
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
![]() |
Similar Threads
- array of strings (C++)
- read to end of line problem (C)
- functions using strings and arrays (C++)
- Learning PHP but problem with script (PHP)
- Strings in array [I NEED HELP] C lang. (C)
- C problem in strings and recursive check (C)
- Finding and Replacing Strings (C++)
Other Threads in the C Forum
- Previous Thread: string array problem
- Next Thread: How to access these elements in C
Views: 589 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
api arguments array arrays binary binarysearch c++ char character code codes coke command conversion convert copy copyimagefile cpu database decimal directory dude dynamic ebook ebooks error exec factorial fgets file fork function functions getlasterror givemetehcodez grade graphics homework i/o input insert int integer lazy libcurl line linked linkedlist linux list lists locate logical_drives loop loops malloc matrix memory messagebox motherboard mysql no-effort opensource output path pause pointer pointers problem process program programming questions read recursion recursive recv reverse scanf single socketprograming socketprogramming spoonfeeding sql static string strings strtok structures student suggestions system systemcall turbo turbo-c turboc unix user variable windows






