#6256: NA


J99 (J)

學校 : 國立中正大學
編號 : 7447
來源 : [114.35.165.69]
最後登入時間 :
2012-05-12 15:32:46
d430. 第二題: 計算字數 (count) -- 92學年度北基區資訊學科能力競賽 | From: [61.216.120.108] | 發表日期 : 2012-01-26 18:26

第2和第3個測資點RE
Segmentation fault
第5個測資點WA
我的答案8
正確答案1
/////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int count(char *buf)
{
int i,sum=0;
char *p=NULL;
p=strtok(buf," \t\n");
for(i=0;p[i]!='\0';i++)
{
if(isalnum(p[i]))
{
sum++;
break;
}
}
while(p=strtok(NULL," \t\n"))
{
for(i=0;p[i]!='\0';i++)
{
if(isalnum(p[i]))
{
sum++;
break;
}
}
}
return sum;
}

int main()
{
int counter=0;
char buf[1024];
memset(buf,'\0',sizeof(buf));
while(fgets(buf,1024,stdin)!=NULL)
{
counter+=count(buf);
memset(buf,'\0',sizeof(buf));
}
printf("%d\n",counter);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
SEGMENTATION FAULT會是因為使用strtok()跑for迴圈的時候超過了嗎??

 
ZeroJudge Forum