By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

CS Student need help with debugging string program

P: 3
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
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
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

P: 3
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 2.5K+
P: 3,652
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

P: 3
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.