关于C编程 count word

来源:百度知道 编辑:UC知道 时间:2024/05/16 05:55:41
Write a program called letters.c to count specified letters of the alphabet in input text.
By default (user just types 'letters'), the program reads and counts all alphabetical characters (letters) from stdin until an EOF character - user types either ctrl-D in Unix or ctrl-Z in a Windows console, or the end of a redirected file is reached. At the end of input, the program prints a table of all 26 letters and their counts - the total count is printed at the end.
The user identifies an option by typing a hyphen ('-') and a valid option character (I, X, V, C, or R). See this usage summary (actually the header comment of our solution) to learn the meaning of each option. [Note: where the usage summary refers to '-A' it means All letters, not an option.]
As described in the usage summary, the options control which letters to count.

#include"stdio.h"
#include"string.h"
void main()
{
char a[10000];
int i,j=1,k;
printf("请输入相应的文段并以回车结束:\n");
gets(a);
k=strlen(a);
for(i=0;i<k;i++)
{
if(a[i]==' ')j++;
}
printf("%d",j);
}

如果这是一道老师出的题 

那么他的目的很显然他是在考查你对C的熟练程度 

思路应该是以空格,标点作为结束符,但这又有了一个问题,第一行letters.c如果算作一个单词的话,那还得以更周密的判断来Count,比如句号后面应该是大写,那么才算作是一个单词(不过这仍然不可行,letters.C也是合法的,文件的扩展名大小写都可以,所以,这种情况无解.但按照本题的意思,letters.c只会输入成小写,这就为判断提供了可能). 

如果实际遇到这种情况的时候 

标准的解决方法应该是使用正则 

我们把Ctrl-D算作一个单词的话,数字26也认为是一个单词的话 

JAVACODE: 

package com.lnu.wordCount; 

import java.util.regex.Matcher; 

import java.util.regex.Pattern; 

public class Main { 

public static void main(String[] args) { 

  String str = &q