Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A question from my data sturture project

  1. May 26, 2006 #1
    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 let;
    int num;
    num= strlen(stun);
    if(let=='.' ||let==',' || let==':')
    if(!(name=(char*)malloc( strlen(stun)+1, sizeof(char))))
    printf("problem allocating name\n");
    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: May 26, 2006
  2. jcsd
  3. May 27, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, to be elegant you have to first stop swearing. :tongue:

    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. :rofl:
    Last edited: May 27, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook