Posted on 2012-08-11 23:11
hoshelly 閱讀(575)
評論(0) 編輯 收藏 引用 所屬分類:
Programming
輸入若干個單詞,輸出它們的平均長度。單詞只包括大寫字母和小寫字母,用一個空格隔開。
代碼測試通過:
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000];
char *p;
p=&s[0];
int len=0,tot=0,ws=0,per_len,i;
gets(s);
len=strlen(s);
for(i=0;i<=len;i++)
{
if( *p >='a' && *p<='z' || *p >='A' && *p<='Z')
{
tot++;
p++;
}
else if( *p == ' ' || *p =='\0')
{
ws++;
p++;
}
}
per_len=tot/ws;
printf("%d\n",per_len);
return 0;
}