English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

Suppose 我有個file proverbs.txt 入面有堆咁既野
1. Between the devil and the seep sea
To choose between two equally bad alternatives
in a serious dilema.

2. Where there's a will, there's a way
When a person really wants to do something,
he will find a way of doing it.
我點可以count佢有幾多個WORDS
仲有我點可以copy淨係第一句去一個新file
i.e, 新file 內容要係咁
1. Between the devil and the seep sea
2. Where there's a will, there's a way
不可用stack

2006-11-26 03:17:24 · 1 個解答 · 發問者 ? 1 in 電腦與網際網路 程式設計

1 個解答

/* word count - Note: the include statements have been modified by the display and have to be corrected*/
#include stdio.h
#include stdlib.h
#include string.h
#include ctype.h
int main(int argc, char *argv[]){
char li[300];
char *p;
int nwords=0;
FILE *f1=fopen("proverbs.txt","r");
FILE *f2=fopen("newfile.txt","w");
while(fgets(li,299,f1)!=NULL){
if(isdigit(li[0])){// digit=proverb, write to file
fprintf(f2,"%s",li);
}
p=strtok(li," ");
if(p)nwords++;// first word
while((p=strtok(NULL," "))!=NULL)nwords++;// subsequent words
}
printf("There are %d words\n",nwords);
fclose(f1);
fclose(f2);
return 0;
}

2006-11-30 07:46:59 · answer #1 · answered by p 6 · 0 0

fedest.com, questions and answers