Esempio n. 1
0
/* count C keywords */
int main(int argc, char *argv[])
{
    int n;
    char word[MAXWORD];

    while (mgetword(word, MAXWORD) != EOF) 
        if (isalpha(word[0])) 
            if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                keytab[n].count++;
            for (n = 0; n < NKEYS; n++)
                if (keytab[n].count > 0)
                    printf("%4d %s\n", keytab[n].count, keytab[n].word);
                return 0;
            
}
Esempio n. 2
0
int main()
{
	int n; // number of words in the array
	char *word = (char*)malloc(sizeof(MAXWORD));
	int k, cond, found;
	char c;
	//////////////////////////////
	for (n = 0; n < LIMIT && ((c = mgetword(word, MAXWORD)) != EOF); n++)
	{
		found = -1;// the word is different from the array
				   //printf("%d", isalpha(word[0]));
		if (isalpha(word[0])) {

			for (k = 0; found == -1 && k < n; k++)//ako ne e namerena i ima oste elementi ot masiva pulni sdumi
			{
				cond = strcmp(keytab[k].word, word);
				//printf("cond= %d\n", cond);
				if (cond == 0) {
					found = k;
					break;
				}

			}

			//printf("found= %d\n", found);


			if (found<0) {
				//novata duma ne e sred purvite n dumi
				keytab[n].count = 1;
				keytab[n].word = (char*)malloc(sizeof(word));

				keytab[n].word = word;

				//printf("After copying %s\n\n", keytab[n].word);
			}
			else {
				//novata duma suvpada s nyakoya ot  purvite n dumi
				keytab[found].count++;
				//printf("keytab[found].count= %d\n", keytab[found].count);
				// na tazi s koyato suvpada se uvelichava broya
				if (n>0)n--; // za da nyama prazen element ot masiva
			}
		}
		word = (char*)malloc(sizeof(MAXWORD));
	}

	print(n);
	/////////////

	size_t structs_len = sizeof(keytab) / sizeof(struct key);

	/* sort array using qsort functions */
	qsort(&keytab, structs_len, sizeof(struct key*), struct_cmp_by_count);


	///////////////	

	getchar();
	return 0;

}