467,858 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

CS Student need help with debugging string program

Hi, I'm trying to write a small code for an assignment which the void String Tokenizer will get a line input, and take out those delimiters from the original string. Then pass each string without the delimiters back to the struct.

For an example:
Line in is: Today, Friday 23, is a crazy day.
Delimiters is: " ,." /*space, comma and period/*
The stuct will get total of 7 words: "Today", "Friday", "23", "is" ..... "day".

This is the codes for that little function:
Expand|Select|Wrap|Line Numbers
  1. void constructStringTokenizer(struct StringTokenizer* tokenizer, const char line[], const char delimiters[])
  2. {
  3. char* temp;
  4. int i, j, k;
  5. k=0;
  6. for (i =0; i < strlen(line); i++)
  7. {
  8.    if (k == 0)
  9.    temp = malloc (sizeof(char)*MAXLEN+1);
  10. // printf ("%d\n", i);
  11.    for (j = 0; j < 3; j++)
  12.    {
  13. //     printf("Went through one delimiter\n");
  14.        if (line[i]==delimiters[j])
  15.        {
  16.            i++;
  17.            tokenizer->tokens[tokenizer->count] = (char*)malloc(sizeof(char)*(k+1));
  18.            strcpy (tokenizer->tokens[tokenizer->count++],temp);
  19.            strcpy (temp, "");
  20.            k = 0;
  21.            printf ("One string copied\n");
  22.            break;
  23.         }
  24.     }
  25.     temp[k]=line[i];
  26.     k++;
  27.     printf ("%s\n", temp);
  28. }
  29. //    free (temp);
  30. }
  31.  
I tested each string passed back into the struct, and first 2 strings are fine, but all other strings are really messed up, with weird characters at the end. At the same time, it skipped last 2 words. Is something wrong with my algorithm? Is there a alternate algorithm for doing this?

Thanks.
Feb 23 '07 #1
  • viewed: 1181
Share:
4 Replies
Ganon11
Expert 2GB
One thing I spotted was that, when you use malloc to set temp, you don't cast it to a char* a.k.a.

Expand|Select|Wrap|Line Numbers
  1. temp = (char*)malloc(sizeof(char) * MAXLENGTH + 1)
Also, you only free temp at the end of the function, though it may be possible that temp has a new array allocated to it several times, leaving several blocks of char arrays in memory.
Feb 23 '07 #2
Thank you for quick replying. Yeah, I noticed the (char*)malloc error. I thought that since that token in struct Tokenizer is also a (char*), I need that to memory allocate approriately.

Also, I'm using VC++ 6.0, for some reason, it doesn't let me free temp, even at the end. That's why I didn't free temp, I opted that code out using //.
Feb 23 '07 #3
Ganon11
Expert 2GB
Well, I know the new standard is to use new and delete rather than malloc and free, so maybe VC++ is only letting you use delete instead of free...
Feb 23 '07 #4
Hi, I had found and fixed the bug. It does everything I want it to do, except one thing. I believe there's an error in allocating the string. I tried to find the error in coding, but no avail. Each string copied into the struct was fine, however, at the end of each string, there are 4 extra 'weird' characters -- which are not suppose to be there. Can you please check my algorithm and codes?

Expand|Select|Wrap|Line Numbers
  1. void constructStringTokenizer(struct StringTokenizer* tokenizer, const char line[], const char delimiters[])
  2. {
  3. char* temp;
  4. int i, j, k;
  5. k=0;
  6. temp = malloc (sizeof(char)*MAXLEN+1);
  7. for (i =0; i < strlen(line); i++)
  8. {
  9.     for (j = 0; j < 3; j++)
  10.     {
  11.         if (line[i]==delimiters[j])
  12.         {
  13.            tokenizer->tokens[tokenizer->count] = malloc(sizeof(char)*(k));
  14.            strncpy (tokenizer->tokens[tokenizer->count++],temp, k);
  15.            strcpy (temp, " ");
  16.            k = 0;
  17.            i++;
  18.            break;
  19.         }
  20.     }
  21. if (line[i] == delimiters[0])
  22. i++;
  23. temp[k]=line[i];
  24. k++;
  25. }
  26. free (temp);
  27. }
  28.  
Feb 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by mchoya | last post: by
31 posts views Thread by Martin J√łrgensen | last post: by
2 posts views Thread by sallyk07 | last post: by
11 posts views Thread by xxbabysue123xx | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.