CS Student need help with debugging string program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nnguyec
    New Member
    • Feb 2007
    • 3

    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:
    Code:
    void constructStringTokenizer(struct StringTokenizer* tokenizer, const char line[], const char delimiters[])
    {
    char* temp;
    int i, j, k;
    k=0;
    for (i =0; i < strlen(line); i++)
    {
       if (k == 0)
       temp = malloc (sizeof(char)*MAXLEN+1);
    // printf ("%d\n", i);
       for (j = 0; j < 3; j++)
       {
    //     printf("Went through one delimiter\n");
           if (line[i]==delimiters[j])
           {
               i++;
               tokenizer->tokens[tokenizer->count] = (char*)malloc(sizeof(char)*(k+1));
               strcpy (tokenizer->tokens[tokenizer->count++],temp);
               strcpy (temp, "");
               k = 0;
               printf ("One string copied\n");
               break;
            }
        }
        temp[k]=line[i];
        k++;
        printf ("%s\n", temp);
    }
    //	free (temp);
    }
    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    One thing I spotted was that, when you use malloc to set temp, you don't cast it to a char* a.k.a.

    Code:
    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.

    Comment

    • nnguyec
      New Member
      • Feb 2007
      • 3

      #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 //.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        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...

        Comment

        • nnguyec
          New Member
          • Feb 2007
          • 3

          #5
          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?

          Code:
          void constructStringTokenizer(struct StringTokenizer* tokenizer, const char line[], const char delimiters[])
          {
          char* temp;
          int i, j, k;
          k=0;
          temp = malloc (sizeof(char)*MAXLEN+1);
          for (i =0; i < strlen(line); i++)
          {
              for (j = 0; j < 3; j++)
              {
                  if (line[i]==delimiters[j])
                  {
                     tokenizer->tokens[tokenizer->count] = malloc(sizeof(char)*(k));
                     strncpy (tokenizer->tokens[tokenizer->count++],temp, k);
                     strcpy (temp, " ");
                     k = 0;
                     i++;
                     break;
                  }
              }
          if (line[i] == delimiters[0])
          i++;
          temp[k]=line[i];
          k++;
          }
          free (temp);
          }

          Comment

          Working...