在计数在C段的句子数量 [英] counting the number of sentences in a paragraph in c
问题描述
由于我的课程的一部分,我一直在使用学习C的Turbo C(不幸)。
我们的老师要求我们做一件code的计数在段落的字,词,句的数量(只用printf,则getch()和while循环..他不希望我们使用任何其他命令尚未)。这里是code我写的:
的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;无效的主要(无效)
{
clrscr();
诠释计数= 0;
INT字= 0;
INT句子= 0;
焦炭CH;而((CH =残培())!='\\ n')
{
的printf(%C,CH);
而((CH =的getch())!='。')
{
的printf(%C,CH);
而((CH =残培())!='')
{
的printf(%C,CH);
算上++;
}
的printf(%C,CH);
也就是说++;
}
句子++;
}的printf(字符数是%D,计数);
的printf(字样的\\ n此数量%D字);
的printf(句\\ n此数量%D,句子);
残培();
}
它的工作(计至少字符和文字的数量)。然而,当我编译code和检查它在控制台窗口中,我不能让程序停止运行。它应该只要我输入回车键结束。这是为什么?
INT CH;
INT标志;
而((CH =残培())!='\\ r'){
++计数;
标志= 1;
而(旗&安培;及(CH ==''|| CH =='')){
++字; //没有好的例如连续空间,空间在句子的开头
标志= 0 ;;
}
标志= 1;
而(旗&安培;&放大器;'。CH ==){
++句子;
标志= 0;
}
的printf(%C,CH);
}
的printf(\\ n);
As part of my course, I have to learn C using Turbo C (unfortunately).
Our teacher asked us to make a piece of code that counts the number of characters, words and sentences in a paragraph (only using printf, getch() and a while loop.. he doesn't want us to use any other commands yet). Here is the code I wrote:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int count = 0;
int words = 0;
int sentences = 0;
char ch;
while ((ch = getch()) != '\n')
{
printf("%c", ch);
while ((ch = getch()) != '.')
{
printf("%c", ch);
while ((ch = getch()) != ' ')
{
printf("%c", ch);
count++;
}
printf("%c", ch);
words++;
}
sentences++;
}
printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
}
It does work (counts the number of characters and words at least). However when I compile the code and check it out on the console window I can't get the program to stop running. It is supposed to end as soon as I input the enter key. Why is that?
int ch;
int flag;
while ((ch = getch()) != '\r'){
++count;
flag = 1;
while(flag && (ch == ' ' || ch == '.')){
++words;//no good E.g Contiguous space, Space at the beginning of the sentence
flag = 0;;
}
flag = 1;
while(flag && ch == '.'){
++sentences;
flag=0;
}
printf("%c", ch);
}
printf("\n");
这篇关于在计数在C段的句子数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!