Programming Problem in C language

2006-11-26 4:17 pm
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

回答 (1)

2006-11-30 8:46 pm
✔ 最佳答案
/* 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;
}


收錄日期: 2021-04-12 20:08:42
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061126000051KK00810

檢視 Wayback Machine 備份