Example #1
0
File: sort.c Project: robinrob/c
void do_sort_words(char *w[], int n)
{
	printf("\nsort_words:\n");
	start_time();
	sort_words(w, n);
	print_time();
}
Example #2
0
int main()
{
    char words_to_sort[WORDS][10] = {"Shoe", "Hat", "Cat", "Bat", "Pencil", "Coffee"};
    
    sort_words(words_to_sort);
    
    return 0;
}
Example #3
0
int main(int argc, char **argv) {
	if(argc == 1) {
		printf("please run this program with at least one argument.\n");
	}
	else{
		int i;
		char buffer[BUFLEN];
		char* saved_words[]= {"int","char","if","else","while","for","return",(char*)0};
		namelist file_words= make_namelist();
		for (i=1; i<argc; ++i){
		      FILE *fptr;
		      if ( (fptr = fopen(argv[i],"r")) == NULL)
			    printf ("file %s couldn't be opened\n",argv[i]);
		      else {
			    char word[BUFLEN];
			    int ich = 0;
			    int isaved = 0;
			    int inword = 0;
			    for(;;) {
				int ch = fgetc(fptr);
				if(feof(fptr))
				      break;
				if(inword) {
				      if(isalpha(ch)) {    // adding char to the word
					  word[ich] = ch;
					  ++ich;
				      } 
				      else{     // just finished reading a word
					  word[ich] = 0;
					  ich = 0;
					  inword = 0;
					  if (!find_word(saved_words,word)){
						add_name(file_words,word);
					  }
	
				      }
				}
		
				else {
				      if(isalpha(ch)) {        //for the first char of the word
					  ungetc(ch,fptr);
					  inword=1;
				      } 
				      if (ch == '\"'){
					  ch = fgetc(fptr);
					  while (ch != '\"')
						ch = fgetc(fptr);
				      }
				}
			    }
		      }
		}
		sort_words(file_words);
		print_words(file_words);
	}
	return 0;
}
Example #4
0
File: game.c Project: orodley/ttc
void new_level(Game *game)
{
    char *word = strdup(random_word(game->all_words_array, MAX_WORD_LENGTH));

    printf("word is %s\n", word);
    shuffle(word);
    printf("word is %s\n", word);

    game->anagrams = find_all_anagrams(game->all_words_tree, word);
    printf("%zu anagrams:\n", game->anagrams->count);

    WordList **anagrams_by_length = malloc(sizeof(*anagrams_by_length) *
                                           (MAX_WORD_LENGTH - 2));
    sort_words(anagrams_by_length, game->anagrams);
    for (size_t i = 0; i < MAX_WORD_LENGTH - 2; i++) {
        printf("%zu: %zu words\n", i + 3, anagrams_by_length[i]->count);

        for (size_t j = 0; j < anagrams_by_length[i]->count; j++)
            printf("\t%s\n", GET_WORD(anagrams_by_length[i], j));
    }

    game->anagrams_by_length = anagrams_by_length;

    game->column_sizes = compute_layout(game);

    game->guessed_words =
        make_word_list(game->anagrams->count, MAX_WORD_LENGTH + 1);

    SDL_Color white = { 0xFF, 0xFF, 0xFF, 0xFF };
    game->guessed_words_texture = render_empty_words(game, white);
    SDL_DestroyTexture(game->message_box);
    game->message_box = NULL;

    game->chars_entered = 0;

    memset(game->curr_input, 0, MAX_WORD_LENGTH + 1);
    memcpy(game->remaining_chars, word, MAX_WORD_LENGTH + 1);

    if (game->second_timer != 0)
        SDL_RemoveTimer(game->second_timer);
    game->second_timer = SDL_AddTimer(1000, timer_handler, game);
    game->time_left = 180;

    game->state = IN_LEVEL;
}
Example #5
0
File: fvec.c Project: MLDroid/sally
/**
 * Extracts word n-grams from a string. The features are represented 
 * by hash values.
 * @param fv Feature vector
 * @param x Byte sequence 
 * @param l Length of sequence
 * @parma nlen N-gram len
 * @param pos Positional n-grams
 * @param shift Shift value
 */
static void extract_wgrams(fvec_t *fv, char *x, int l, int nlen, int pos,
                           int shift)
{
    assert(fv && x && l > 0);
    int sort, sign, flen;
    cfg_int bits;
    unsigned int i, j = l, ci = 0;
    unsigned int dlm = 0;
    unsigned int fstart, fnext = 0, fnum = 0;
    char *t = malloc(l + 1), *fstr;
    fentry_t *cache = NULL;

    /* Get configuration */
    config_lookup_bool(&cfg, "features.ngram_sort", &sort);
    config_lookup_int(&cfg, "features.hash_bits", &bits);
    config_lookup_bool(&cfg, "features.vect_sign", &sign);

    /* Set bits of hash mask */
    feat_t hash_mask = ((long long unsigned) 2 << (bits - 1)) - 1;

    if (fhash_enabled())
        cache = calloc(l, sizeof(fentry_t));

    /* Find first delimiter symbol */
    for (dlm = 0; !delim[(unsigned char) dlm] && dlm < 256; dlm++);

    /* Remove redundant delimiters */
    for (i = 0, j = 0; i < l; i++) {
        if (delim[(unsigned char) x[i]]) {
            if (j == 0 || delim[(unsigned char) t[j - 1]])
                continue;
            t[j++] = (char) dlm;
        } else {
            t[j++] = x[i];
        }
    }

    /* No characters remaining */
    if (j == 0)
        goto clean;

    /* Add trailing delimiter */
    if (t[j - 1] != dlm)
        t[j++] = (char) dlm;

    /* Extract n-grams */
    for (fstart = i = 0; i < j; i++) {
        /* Count delimiters and remember start position */
        if (t[i] == dlm && ++fnum == 1)
            fnext = i;

        /* Store n-gram */
        if (fnum == nlen && i - fstart > 0) {
            /* Copy feature string and add slack */
            flen = i - fstart;
            fstr = malloc(flen + sizeof(unsigned long));
            memcpy(fstr, t + fstart, flen);

            /* Sorted n-grams code */
            if (sort)
                fstr = sort_words(fstr, flen, dlm);

            /* Positional n-grams code */
            if (pos) {
                int32_t p = ci + shift;
                memcpy(fstr + flen, &p, sizeof(int32_t));
                flen += sizeof(int32_t);
            }

            feat_t h = hash_str(fstr, flen);
            fv->dim[fv->len] = h & hash_mask;
            fv->val[fv->len] = 1;

            /* Signed embedding */
            if (sign)
                fv->val[fv->len] *= (signed) h > 0 ? -1 : 1;

            /* Cache feature and key */
            if (fhash_enabled())
                cache_put(&cache[ci], fv, fstr, flen);

            fstart = fnext + 1, i = fnext, fnum = 0;
            fv->len++;
            ci++;
            free(fstr);
        }
    }

    /* Save extracted n-grams */
    fv->total += fv->len;

  clean:
    if (fhash_enabled()) {
        cache_flush(cache, ci);
        free(cache);
    }
    free(t);
}