Thread: Text parser + hashing + dictionary

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Text parser + hashing + dictionary

    Everytime I run this code, the process never ends... maybe there's something wrong with the code.

    The code is about picking a text, making a list with every word of the text, then making use of a hashfunction (R&K) to create a dictionary with one instance each word, and printing the whole table.

    Here's the code.


    Thanks in advice,
    Lucas.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct list {
    
        list* next;
        char word[40];
    
    };
    
    
    
    struct hashtable{
    
        char word[40];
    
    };
    
    // Structs defined
    
    
    unsigned int hashfunction(char *s) {
    
             unsigned int hashval;
    
                      for (hashval = 0; *s != '\0'; s++){
    
                     hashval = *s + 31 * hashval;
                      }
    
    
            return hashval % 5000;
    
    }
    
    // Not sure if the hashfunction is correct. Got that at the internet.
    
    
    list* parser(char* c){
    
        list *p = (list*) malloc(sizeof(list));
        list *q = p;
        char word[40]="";
        int i = 0;
        int j = 0;
    
    
        while (c[i+1]!="\0"){
    
    
            if(c[i] != "," || c[i] != "." || c[i] != " " || c[i] != "/"){
    
                word[j]=c[i];
                j++;
    
            }
    
    
    
            if( c[i] == "," || c[i] == "." || c[i] == " " || c[i] == "/"){
    
                strcpy(word,q->word);
                q=q->next;
                j=0;
                memset ((void *) &word, '\0', sizeof(word));
    
            }
    
    
    
            i++;
    
        }
    
    
        return p;
    
    }
    
    // Parser implemented
    
    
    hashtable* hashing(hashtable* table, list* l){
    
        list* p = l;
        unsigned int val;
    
    
        while(p->next!=NULL){
    
    
            val = hashfunction(p->word);
    
    
            if(table[val].word==NULL){
                strcpy(p->word, table[val].word);
            }
    
            p=p->next;
    
        }
    
    
        return table;
    
    }
    
    
    // Hashing process implemented.
    
    
    
    
    int main() {
    
        char text[50000];
    
        hashtable table[5000];
    
        printf("What's your text? ");
    
        gets(texto);
    
        hashing(table,parser(text));
    
    
        for (int i=0;i < 5000;i++){
    
            if(table[i].word!='\0'){
                printf("%c", table[i].word);
              }
       }
    
    
        system("pause");
    
        return 0;
    
    }
    Last edited by LucasDeLima; 10-24-2012 at 09:12 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Everytime I run this code, the process never ends... maybe there's something wrong with the code.
    Yes, maybe there is. You should try to find out what goes wrong and where. If you don't know how to use a debugger yet, start simple and put some printf/couts into your code, so you can see what it does and where it hangs or loops.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing to do is decide whether you're a C programmer, or a C++ programmer.

    Compiled as C (which is what it looks like), there are a lot of things to sort out.
    Code:
    foo.c:7:5: error: unknown type name ‘list’
    foo.c:40:1: error: unknown type name ‘list’
    foo.c: In function ‘parser’:
    foo.c:42:5: error: unknown type name ‘list’
    foo.c:42:16: error: ‘list’ undeclared (first use in this function)
    foo.c:42:16: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:42:21: error: expected expression before ‘)’ token
    foo.c:43:11: error: ‘q’ undeclared (first use in this function)
    foo.c:49:18: warning: comparison between pointer and integer [enabled by default]
    foo.c:52:17: warning: comparison between pointer and integer [enabled by default]
    foo.c:52:32: warning: comparison between pointer and integer [enabled by default]
    foo.c:52:47: warning: comparison between pointer and integer [enabled by default]
    foo.c:52:62: warning: comparison between pointer and integer [enabled by default]
    foo.c:61:18: warning: comparison between pointer and integer [enabled by default]
    foo.c:61:33: warning: comparison between pointer and integer [enabled by default]
    foo.c:61:48: warning: comparison between pointer and integer [enabled by default]
    foo.c:61:63: warning: comparison between pointer and integer [enabled by default]
    foo.c: At top level:
    foo.c:84:1: error: unknown type name ‘hashtable’
    foo.c:84:20: error: unknown type name ‘hashtable’
    foo.c:84:38: error: unknown type name ‘list’
    foo.c: In function ‘main’:
    foo.c:119:5: error: unknown type name ‘hashtable’
    foo.c:123:10: error: ‘texto’ undeclared (first use in this function)
    foo.c:128:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
    foo.c:128:5: note: use option -std=c99 or -std=gnu99 to compile your code
    foo.c:130:20: error: request for member ‘word’ in something not a structure or union
    foo.c:131:34: error: request for member ‘word’ in something not a structure or union
    Compiled as C++ (because that's where you posted), there are fewer errors
    Code:
    $ g++ foo.c
    foo.c: In function ‘list* parser(char*)’:
    foo.c:49:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:52:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:52:35: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:52:50: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:52:65: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:61:21: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:61:36: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:61:51: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c:61:66: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
    foo.c: In function ‘int main()’:
    foo.c:123:10: error: ‘texto’ was not declared in this scope
    foo.c:131:39: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char*’ [-Wformat]
    > if(c[i] != "," || c[i] != "." || c[i] != " " || c[i] != "/")
    Perhaps you meant single quotes on all of these single characters.

    And I've no idea where you got "texto" as a variable name. It certainly isn't the code you "compiled and run", because it doesn't even compile!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hmm...based on the code, I would like to make a wager on the originating country.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Quote Originally Posted by Salem View Post
    And I've no idea where you got "texto" as a variable name. It certainly isn't the code you "compiled and run", because it doesn't even compile!
    Forgot to translate "texto". It means "text".

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I think the project is too complicated for your level.You should try something simpler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-29-2012, 09:52 AM
  2. Dictionary text file where?
    By adr in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-05-2006, 05:00 AM
  3. seg fault in text parser
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 11-04-2005, 08:00 AM
  4. Comparing a text file with a dictionary file help!!
    By scotty170185 in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2004, 10:00 PM
  5. May I buy you a dictionary?
    By EvenFlow in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 10-19-2001, 12:58 PM