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: - 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.
4 1455
One thing I spotted was that, when you use malloc to set temp, you don't cast it to a char* a.k.a. - 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.
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 //.
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...
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? -
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);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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++...
|
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...
|
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...
|
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...
|
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...
|
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”. ...
|
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...
|
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...
|
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:
-------------------------------------------------------
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
| |