promotion image of download ymail app
Promoted

c語言程式

Given a document stored as a string:

char document[]="National Taiwan Ocean University was originally established in 1953 as a junior college for the study of maritime science and technology.\nAfter eleven years, in 1964, we became a maritime college which offered bachelor's and master's degrees in various fields of maritime studies. During this period, funds for running the college came from the Taiwan Provincial Government of the Republic of China.\nIn 1979 the national government took over the funding and we became the National Maritime College. After another decade, in 1989, the college grew into a full-fledged university, National Taiwan Ocean University (NTOU).";

Count and output the frequencies of the following words:

National 4

Taiwan 3

Ocean 2

University 3

was 1

originally 1

established 1

in 5

...

NTOU 1

[Hint]

Use strtok() to separate tokens. Choose appropriate separation symbols.

Declare a large array of pointers to characters. Whenever you find a new word, save its address in the array.

Declare an array of integers to count the occurrences of the corresponding strings.

You can use stricmp() to perform case-insensitive string comparison.

[Possible Algorithm]

Declare an array of pointers of characters wordlist[] to store words.

Declare an integer array frequency[] to store the frequencies of words.

Declare an integer variant wTotal to count the number of seen words. Set it as 0 initially.

Loop for every token in the text

Loop for every seen word wordlisti. If it is the same as token, break out this loop.

If the loop terminates at some i smaller than wTotal// which means that another occurrence of wordlisti is found

Increase frequencyi by 1.

Otherwise,// a new word is found

Let wordlistwTotal point to the same string as token does.

Set frequencywTotal as 1.

Increase wTotal by 1.

End of the loop for token.

Print out the

1 個解答

評分
  • 3M
    Lv 6
    7 年前
    最佳解答

    #include <stdio.h>#include <string.h>#include <ctype.h>int strIcmp(const char *s1, const char *s2){int ret = 0;while (!(ret =tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2)) && *s2) ++s1, ++s2;if (ret < 0)ret = -1;else if (ret > 0)ret = 1 ;return ret;}int toFindList(char *token,char *wordlist[16],int frequency[],int *wTotal){int i;for(i=0;i<(*wTotal);i++){if (!strIcmp(token,wordlist[i])) {frequency[i]++;return 1;}}wordlist[*wTotal]=token;frequency[*wTotal]=1;*wTotal = *wTotal +1 ;return 0;}char document[]="National Taiwan Ocean University was originally established in 1953 as a junior college for the study of maritime science and technology.\nAfter eleven years, in 1964, we became a maritime college which offered bachelor's and master's degrees in various fields of maritime studies. During this period, funds for running the college came from the Taiwan Provincial Government of the Republic of China.\nIn 1979 the national government took over the funding and we became the National Maritime College. After another decade, in 1989, the college grew into a full-fledged university, National Taiwan Ocean University (NTOU).";int main (int argc, const char * argv[]) {int i;int frequency[256]={0};int wTotal=0;char * token ;char *wordlist[256];token = strtok(document," ,().\n");while (token!=NULL) {toFindList(token,wordlist,frequency,&wTotal);token = strtok(NULL," ,().\n");}for (i=0; i<wTotal; i++) {printf("%s : %d\n",wordlist[i],frequency[i]);} return 0;}

    2012-12-21 15:53:47 補充:

    我沒有stricmp ,所以借用strcmp的source code再稍加修改

    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。