user-0 提问: 【求助】写一个统计文件中单词和字符个数的程序

回复:
user: user-1
content: k=open("in.txt","r")j=k.read()x=len(j)a=0for i in range(0,x+1):    if 65<=ord(j[i])<=90 or 97<=ord(j[i])<=122:        if ord(j[i+1])<65 or 90<ord(j[i+1])<97 or ord(j[i+1])>122:            a=a+1print(str(a)+" "+str(x-1))

user: user-2
content: int main()                 //写一个统计文件中单词和字符个数的程序。在此，单词指的是仅由字母组成的串。诸如is7years这样的串按照两个单词计算。{ FILE *f; char word[100]; char c; int i =0,len,m=0,n=0; f = fopen("in.txt","r"); while((c=fgetc(f))!=EOF) {                                       word[i++] = c; } word[i]='\0'; len = strlen(word); /*printf("%d\n",len);*/ for(i=0;i<len;i++) {     if(word[i]!='\0') n++;      if((i==len-1) && ((word[i+1]>='a' && word[i+1]<='z') || (word[i+1]>='A' && word[i+1]<='Z')))     {         m++;         break;          }     if(((word[i]>='a' && word[i]<='z') || (word[i]>='A' && word[i]<='Z')))     {     if(((word[i+1]>='a' && word[i+1]<='z') || (word[i+1]>='A' && word[i+1]<='Z')))     {              continue;     }else{             m++;          }      } } printf("%d %d",m,n); fclose(f);  }

user: user-1
content: k=open("in.txt","r") j=k.read() x=len(j) a=0 for i in range(0,x+1):     if 65&lt;=ord(j[i])&lt;=90 or 97&lt;=ord(j[i])&lt;=122:         if ord(j[i+1])&lt;65 or 90&lt;ord(j[i+1])&lt;97 or ord(j[i+1])&gt;122:             a=a+1 print(str(a)+" "+str(x-1))

user: user-3
content: 写错了

user: user-4
content: 供参考：#include <stdio.h> #include <stdlib.h>  int main() {     char fname[]="in.txt";     char str[1024];     int  i,sum=0,k=0,n;     FILE *fp;     if((fp = fopen(fname,"r")) == NULL){         printf("cannot open the file!");         exit(0);     }     i=0;     while((str[i++]=fgetc(fp))!=EOF);//读入文件到数组     str[i-1] = '\0';     fclose(fp);     n = i;     for(i=0;i<n;i++)  //文件内容输出         printf("%c",str[i]);     for(i=0;i<n;i++){  //计算单词个数         if((str[i]<='z'&&str[i]>='a')||(str[i]<='Z'&&str[i]>='A')) {             k = 1;             continue;         }         if(k) sum++;         if(str[i]>'z'||str[i]<'a'||str[i]>'Z'||str[i]<'A'){             k = 0;             continue;         }     }     printf("\n共%d个单词,共%d个字符\n",sum,n-1);          return 0; }   //I am an (BeiHang)student.is7years //共7个单词,共33个字符 //请按任意键继续. . .

user: user-1
content: k=open("in.txt","r") j=k.read() x=len(j) a=0 for i in range(0,x+1):     if 65&lt;=ord(j[i])&lt;=90 or 97&lt;=ord(j[i])&lt;=122:         if ord(j[i+1])&lt;65 or 90&lt;ord(j[i+1])&lt;97 or ord(j[i+1])&gt;122:             a=a+1 print(str(a)+" "+str(x-1))

user: user-5
content: #include <stdio.h> #include <ctype.h> #include <string.h> #include <errno.h>  #define     FILE_PATH   "in.txt" #define     WORD_LEN    64 #define     METHOD_ONE  int main() {     FILE *fp; #ifdef METHOD_ONE     int flag = 0; #else     int pre_ch = 0; #endif     int ch, word_cnt = 0, ch_cnt = 0;     char word[WORD_LEN];     int i = 0;      fp = fopen(FILE_PATH, "r");     if (!fp) {         fprintf(stderr, "open %s error: %s\n", FILE_PATH, strerror(errno));         return -1;     }      while ((ch = fgetc(fp)) != EOF) {         if (ch == '\n')             continue;         ch_cnt++; #ifdef METHOD_ONE         if (isalpha(ch)) {             flag = 1;             if (i >= WORD_LEN - 1) {                 word[i] = 0;                 puts(word);                 fprintf(stdout, "Word is too length!\n");                 i = 0;                 continue;             }             word[i++] = ch;         } else if (!isalpha(ch) && flag) {             word_cnt++;             word[i] = '\0';             puts(word);             i = 0;             flag = 0;         } #else         if (isalpha(ch)) {             if (i >= WORD_LEN - 1) {                 word[i] = 0;                 puts(word);                 fprintf(stdout, "Word is too length!\n");                 i = 0;                 continue;             }             word[i++] = ch;         }          if (!isalpha(ch) && isalpha(pre_ch)) {             word_cnt++;             word[i] = 0;             i = 0;             puts(word);         }         pre_ch = ch; #endif     }      fclose(fp);      printf("word:%d, ch:%d\n", word_cnt, ch_cnt);      return 0; } 供参考～

user: user-1
content: k=open("in.txt","r") j=k.read() x=len(j) a=0 for i in range(0,x+1):     if 65&lt;=ord(j[i])&lt;=90 or 97&lt;=ord(j[i])&lt;=122:         if ord(j[i+1])&lt;65 or 90&lt;ord(j[i+1])&lt;97 or ord(j[i+1])&gt;122:             a=a+1 print(str(a)+" "+str(x-1))