C字数程序 [英] C Word Count program

查看:121
本文介绍了C字数程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,将依靠文本的字,词,行数,文字是:

这是个月黑风高的夜晚;
在大雨如注 - 除了
在偶尔的时间间隔,当它是
风的猛烈阵风检查
席卷了大街小巷(因为它是
在伦敦我们的场景谎言)
沿着房顶剑拔弩张,狠狠地
搅拌灯的火焰寥寥无几
这对挣扎在黑暗中。

爱德华·布尔沃 - 利顿的小说保罗·克利福德。

我不断收到62而不是64,有什么建​​议?

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&文件ctype.h GT;诠释主(){
        INT tot_chars = 0; / *总*字符/
        INT tot_lines = 0; / *总*线/
        INT tot_words = 0; / *总*字/
        INT布尔;
        / * == EOF文件结束* /
        INT N;
        而((N =的getchar())!= EOF){
                tot_chars ++;
                如果(isspace为(N)及&放大器;!isspace为(的getchar())){
                     tot_words ++;
                }
                如果(N =='\\ n'){
                     tot_lines ++;
                }
                如果(正==' - '){
                        tot_words--;
                }
        }
        的printf(行,词,字符\\ n);
        的printf(%3D%3D%3D \\ n,tot_lines,tot_words,tot_chars);        //应该是11 64 375
        // RN是11 65 375
        返回0;
}


解决方案

我检查你的code和它工作得很好,也得到了我,因为它需要的是─输出(总字)
看来code已经从原来的职位编辑

连接输出什么,我运行code-后得到
产量

I am trying to write a program that will count the number of characters, words and lines in a text, the text is:

It was a dark and stormy night; the rain fell in torrents - except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets (for it is in London that our scene lies), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.

Edward Bulwer-Lytton's novel Paul Clifford.

I keep getting 62 instead of 64, any suggestions?

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main() {
        int tot_chars = 0 ;     /* total characters */
        int tot_lines = 0 ;     /* total lines */
        int tot_words = 0 ;     /* total words */
        int boolean;
        /* EOF == end of file */
        int n;
        while ( (n = getchar()) != EOF){
                tot_chars++;
                if (isspace(n) && !isspace(getchar())){
                     tot_words++;
                }
                if (n=='\n'){
                     tot_lines++;
                }
                if (n=='-'){
                        tot_words--;
                }
        }
        printf("Lines, Words, Characters\n");
        printf(" %3d %3d %3d\n", tot_lines,tot_words,tot_chars);

        // Should be 11 64 375
        // rn     is 11 65 375
        return 0 ;
}

解决方案

I check your code and it works fine, also i got the output (total words) as it desired to be- Seems the code has been edited from its original post

Attaching the Output what I got after running the code- Output

这篇关于C字数程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆