用c++ 6.0怎么统计单词跟字符的个数

来源:百度知道 编辑:UC知道 时间:2024/05/27 15:58:07
用Visual C++ 6.0设计一个程序,要能统计一段英文的单词个数跟字母个数。不要太复杂的,简单易懂点。程序要能识别用标点分隔的单词。
有没有再简单点的?在编程中不用出现define的,还有就是里面的prev是什么意思

/***********************************************************************************
第六章随堂练习一
统计字符,单词和行数
**********************************************************************************/
#include<stdio.h>
#include<ctype.h>
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
int inword = 0;

printf("Enter text to be analyzed( | to be quit):\n ");
prev = '\n';
while((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
n_lines++;
if (!isspace(c) && !inword)
{
inword = 1;
n_words++;

}
if(isspace (c) && inword)
inword = 0;
prev = c;
}

if(prev != '\n')
p_lines = 1;
printf("characters = %ld,word = %d,line = %d