Thread: why isn't doing what I want it to do?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    Unhappy why isn't doing what I want it to do?

    Simple...Array of different words, Initial array size is 4. Upon reaching the limit, it shoudl dynamically expand. MY CODE ISN"T DOING THIS. Also, I am trying to qsort it after each addition...well IT ISN:T DOING THSI ALSO... Why? What's wrong and what should I look out for? Here is the code: THANK YOU SOOO MUCH !

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define SPELL_OKAY 0 /* success */
    #define SPELL_FAILURE -1 /* general, unspecified failure */
    #define SPELL_NOMEMORY 1 /* could not allocate more memory */
    #define SPELL_NOTFOUND 2 /* could not find a file */
    #define SPELL_TOOLONG 3 /* a word is too long */
    #define SPELL_INCORRECT 4 /* a word is incorrect */
    #define MAXLENGTH 255

    char *dictionary;
    char *temp;
    int arraySize = 4;
    int count = 0;

    char name[] = "kar";
    char *name2 = name;
    char name3[] = "A";
    char *name33 = name3;



    int check_word(char *word);
    int max_word_len(void);
    int add_word(char *word);
    /*int charcompare(const void *p1, const void *p2);*/

    int main()
    { dictionary = (char *)malloc(arraySize*sizeof(char));
    add_word(name2);
    add_word(name2);
    add_word(name2);
    add_word(name33);
    printf("%s\n", dictionary[3]);
    }

    int charcompare(const void* p1, const void * p2)
    {
    char * i = *((char*)p1);
    char * j = *((char*)p2);

    if (i > j)
    return (1);
    if (i < j)
    return (-1);
    return (0);
    }


    int max_word_length(void)
    { return MAXLENGTH;
    }



    int add_word(char *word)
    { int result;
    if( (result= check_word(word)) != SPELL_OKAY)
    return result;
    else
    { if (count >= arraySize)
    { printf("DEBUG: reallocating dictionary ... \n") ;

    arraySize = 2 * arraySize;
    temp = realloc(dictionary, arraySize * sizeof(char)) ;
    if (temp == NULL)
    { fprintf(stderr, "realloc() failed!\n") ;
    exit(1) ;
    }
    dictionary = temp ;
    }
    dictionary[count] = word ;
    count++ ;

    qsort( (void *)dictionary, count, sizeof(char), charcompare);

    return result;
    }
    }






    int check_word(char *word)
    {
    int i, len;

    /* If length predetermined, check length, if bad, return 0 */
    if (strlen(word) > 10)
    return SPELL_TOOLONG;

    /* Check dictionary first, if found return 3 */
    for (i=0; i<count; i++)
    {
    if ( strcmp(word, dictionary[i]) == 0)
    return SPELL_OKAY;
    }

    len = strlen(word);

    /* Now check to see if all characters are alnums.*/

    for (i=0; i < len; i++)
    {
    if (isalnum(word[i]) == 0)
    return SPELL_INCORRECT;
    }

    /* If we get here, the following is true
    - word is shorter than 10 in length
    - word is not in the dictionary
    - word is made up of all alnum characters
    Therefore, we return 3
    */
    return SPELL_OKAY;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well this is your basic "add words and sort"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define EXTEND_SIZE 20
    
    /* an initial empty dictionary */
    char **dictionary = NULL;
    int  num_words = 0;
    int  max_words = 0;
    
    /* extends the dictionary in steps of 20 words */
    void extend_dictionary ( void ) {
        if ( num_words == max_words ) {
            void *temp;
            int   new_size = max_words + EXTEND_SIZE;
            temp = realloc( dictionary, new_size * sizeof(dictionary[0]) );
            if ( temp != NULL ) {
                dictionary = temp;
                max_words = new_size;
            }
        }
    }
    
    /* make a copy of a string */
    char *dup_string ( char *word ) {
        char *temp = malloc( strlen(word) + 1 );
        if ( temp != NULL ) {
            strcpy( temp, word );
        }
        return temp;
    }
    
    void add_word ( char *word ) {
        extend_dictionary();
        dictionary[num_words++] = dup_string( word );
    }
    
    /* sort words, for use by the qsort function */
    int compare_fn ( const void *a, const void *b ) {
        char * const *pa = a;
        char * const *pb = b;
        return strcmp( *pa, *pb );
    }
    
    int main ( ) {
        int i;
    
        add_word( "Now" );
        add_word( "is" );
        add_word( "the" );
        add_word( "time" );
        add_word( "for" );
        add_word( "all" );
        add_word( "good" );
        add_word( "men" );
    
        qsort( dictionary, num_words, sizeof(dictionary[0]), compare_fn );
    
        for ( i = 0 ; i < num_words ; i++ ) {
            printf( "Word %d is %s\n", i, dictionary[i] );
        }
        return 0; 
    }

Popular pages Recent additions subscribe to a feed