A question from my data sturture project

In summary, the conversation discusses how to scan a text file and save each word into a data structure, while ignoring symbols. The code provided uses a function called AllocateName to do this, but it only works for words ending in a comma or period. The conversation then mentions various string manipulation functions that can be used for this task, but ultimately concludes that string parsing is not elegant.
  • #1
kant
388
0
I have this input file that contain words. I am suppose to scan this text, and save each word into a some data structure( not part of my question). My question is: How do i get the words but ignore the symbols? The text that is given contain symbols like - , : ; ( ) _ - + - ...etc. Here is what i got so far:

while( scanf(fpname, %s, stun) !=EOF) )
{
******
****
***
pname= AllocateName( stun);
*******
*****
***
*

}
/* here is my allocate name fuction*/

char* AllocateName( char *stun)
{
char*name;
char let;
int num;
num= strlen(stun);
--num;
let=stun[num];
if(let=='.' ||let==',' || let==':')
{
stun[num]='\o';
}
if(!(name=(char*)malloc( strlen(stun)+1, sizeof(char))))
{
printf("problem allocating name\n");
exit(2);
}
strcmp(name, stun);

return name;
}

Yes, yes.. It only care for words that ends with a comma, or a period.
So it is a not a perfect solution. There can be words like:

(log)(base4)(12) <-- consider one word

but not this:

I have the utter-most-hatred-this-lab, where "utter", "most", "hatred", "this" , "lab" are consider individual words, without the god damn '-'. In other word, if i save "utter-most-hatred-this-lab" as a string as stun, than it must be borken up into some unknown number of pieces individually allocate in the heap! unless i am to use [^!@#$%^&*()]. What is a alogant way to god damn do this??
 
Last edited:
Technology news on Phys.org
  • #2
Well, to be elegant you have to first stop swearing. :-p

Do you know the strpbrk function? Or strcspn? There's also strtok, but it has issues.

There's a whole slew of useful string manipulation routines -- you should find a chapter in a book on them and just read it, or scan through all the manpages for them (they usually contain references to each other) to learn what they all do.

You can even do a google search for "man strpbrk" :biggrin:


But, the whole lesson is: string parsing is not elegant. Deal with it. :smile:
 
Last edited:
  • #3



To ignore symbols and break up words that contain symbols, you can use the strtok() function in C. This function allows you to specify a set of delimiter characters, in this case symbols, and it will break up a string into smaller strings based on those delimiters. For example, you can use the symbols listed in the input file as delimiters and strtok() will break up words like "utter-most-hatred-this-lab" into individual words without the symbols. You can then save these individual words into your data structure. Here is an example of how you can use strtok() in your code:

while (scanf(fpname, "%s", stun) != EOF) {
char *token = strtok(stun, "-,.;:()_+- ");
while (token != NULL) {
// save token into your data structure
token = strtok(NULL, "-,.;:()_+- ");
}
}

This code will break up the input string "utter-most-hatred-this-lab" into "utter", "most", "hatred", "this", and "lab" and save each of them individually into your data structure. You can also add other symbols to the delimiter list as needed.
 

Related to A question from my data sturture project

1. What is a data structure project?

A data structure project is a project that involves organizing and managing data in a specific way to facilitate efficient storage, retrieval, and manipulation of the data.

2. What are some common data structures used in a data structure project?

Some common data structures used in a data structure project include arrays, linked lists, stacks, queues, trees, and graphs.

3. How do you choose the appropriate data structure for a project?

The appropriate data structure for a project depends on the type of data being stored, the operations that need to be performed on the data, and the efficiency of the data structure for those operations. It is important to carefully consider these factors before selecting a data structure for a project.

4. Can data structures be combined or modified in a project?

Yes, data structures can be combined or modified in a project to better suit the specific needs of the project. For example, a linked list can be combined with a stack to create a linked stack data structure.

5. What are some real-world applications of data structures in projects?

Data structures are used in a variety of real-world applications such as databases, file systems, web development, and artificial intelligence. They are also essential in data analysis and data processing tasks.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
  • General Discussion
Replies
2
Views
3K
Replies
34
Views
5K
Back
Top