473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
4 1455
Ganon11
3,652 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
3,652 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

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

Similar topics

4
by: mchoya | last post by:
I'm so frustrated. I'm beginning school next week and I have been working on a simple program for several days now without being able to complete it. (Though I have brushed up on a lot of C++...
9
by: Dadio | last post by:
Hi! I have to take some strings from a file and put them in a record... The various strings in the file are written on this way: string1|string2|string3|string4|string5| This is the program...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
7
by: RallyDSM | last post by:
Hello, I'm currently trying to read a .CSV file and get all the data into an array so I can work with it in the program. Here is what I currently have. Private Sub IntializeData() Dim AL...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
11
by: xxbabysue123xx | last post by:
Heres the problem: Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”. ...
31
by: Warly girl | last post by:
Hi i have a qustion plz help me to understand and solve it Phase One Problem description You are required to implement a student registration system. The system keeps information about the...
3
by: Synapse | last post by:
hi everyone..im trying to create a student list program using linked list that will display all my info of students..but it seems theres a little prob. after i enter my first student the program will...
4
by: withu4ever | last post by:
I learned how to use struct when I try to write a program i face some misstakes which I cant correct This is the program: ------------------------------------------------------- ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.