分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到map比较合适吧,因为map中有 键-值 的关系,可以把字符串设置为键,把出现的个数设置为整型,这样就能够建立起一一对应的关系,不用再判断所在的位置 根据上面自己的理解,今天我写了以下的一部分代码,对哈利波特第一集的这部分文章进行了单词的统计的测试,测试的结果相对良好,没有问题. package pip…
最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" exit fi filename=$ egrep -o "[a-zA-Z]+" $filename | awk '{count[$0]++} END{printf "%-14s %s\n","Word","Count" for(i…
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int len ; int i ; int flag = 0 ; int count = 0 ; char buffer[20] = {'\0'} ; gets(buffer); puts(buffer); for(i = 0 ; i < strlen(buffer) ; i++) { //判断输入字符串中间有没有…
1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',encoding='utf-8') as a_file: for line in a_file: words = re.findall(r'&#\d+;|&#\d+;|&\w+;',line) for word in words: words_dict[word] = words_dict.…
name = input("Enter file:") if len(name) < 1 : name = "input.txt" fhand = open(name) counts = dict() for line in fhand: words = line.split() for word in words: # find the value that key is word, if not, return 0 counts[word] = count…
package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class test { public static void main(String[] args) throws Exception { new test().wordCount(); }//…
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. 统计输入中单词的长度,并且绘制相应的直方图.水平的直方图比较容易绘制,垂直的直方图较困难一些. /* This program was the…
统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) {  char ch;  FILE *fp;  long count=0;    if(argc !=2)  {   printf("文件名是:%s\n",argv[0]);   exit(EXIT_FAILURE);  }  if ((fp=fopen(argv[1],"r…
统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #include<math.h> #include<algorithm> using namespace std; #define LL long long vector<]; set<LL> st; ], link[], ]; int main(void) { LL ans,…
//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #define N 1000 void main(){ ]; ,n,state; //num 用来统计单词的个数 //state 用来记录程序当前是否处于一个单词之中,初值为0,表示不在单词中,值为1,表示正处于在一个单词中 printf("Please input the number of lines for English passage:"…