counting the number of sentences in a paragraph in c - c

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?

Here you have the solution to your problem:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int count = 0;
int words = 0;
int sentences = 0;
char ch;
ch = getch();
while (ch != '\n')
{
while (ch != '.' && ch != '\n')
{
while (ch != ' ' && ch != '\n' && ch != '.')
{
count++;
ch = getch();
printf("%c", ch);
}
words++;
while(ch == ' ') {
ch = getch();
printf("%c", ch);
}
}
sentences++;
while(ch == '.' && ch == ' ') {
ch = getch();
printf("%c", ch);
}
}
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();
}
The problem with your code is that the innermost while loop was consuming all the characters. Whenever you enter there and you type a dot or a newline it stays inside that loop because ch is different from a blank. However, when you exit from the innermost loop you risk to remain stuck at the second loop because ch will be a blank and so always different from '.' and '\n'. Since in my solution you only acquire a character in the innermost loop, in the other loops you need to "eat" the blank and the dot in order to go on with the other characters.
Checking these conditions in the two inner loops makes the code work.
Notice that I removed some of your prints.
Hope it helps.
Edit: I added the instructions to print what you type and a last check in the while loop after sentences++ to check the blank, otherwise it will count one word more.

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");

I think the problem is because of your outer while loop's condition. It checks for a newline character '\n', as soon as it finds one the loop terminates. You can try to include your code in a while loop with the following condition
while((c=getchar())!=EOF)
this will stop taking input when the user presses Ctrl+z
Hope this helps..

You can implement with ease an if statement using while statement:
bool flag = true;
while(IF_COND && flag)
{
//DO SOMETHING
flag = false;
}
just plug it in a simple solution that uses if statements.
For example:
#include <stdio.h>
#include <conio.h>
void main(void)
{
int count = 0;
int words = 1;
int sentences = 1;
char ch;
bool if_flag;
while ((ch = getch()) != '\n')
{
count++;
if_flag = true;
while (ch==' ' && if_flag)
{
words++;
if_flag = false;
}
if_flag = true;
while (ch=='.' && if_flag)
{
sentences++;
if_flag = false;
}
}
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();
}

#include <stdio.h>
#include <ctype.h>
int main(void){
int sentence=0,characters =0,words =0,c=0,inside_word = 0,temp =0;
// while ((c = getchar()) != EOF)
while ((c = getchar()) != '\n') {
//a word is complete when we arrive at a space after we
// are inside a word or when we reach a full stop
while(c == '.'){
sentence++;
temp = c;
c = 0;
}
while (isalnum(c)) {
inside_word = 1;
characters++;
c =0;
}
while ((isspace(c) || temp == '.') && inside_word == 1){
words++;
inside_word = 0;
temp = 0;
c =0;
}
}
printf(" %d %d %d",characters,words,sentence);
return 0;
}
this should do it,
isalnum checks if the letter is alphanumeric, if its an alphabetical letter or a number, I dont expect random ascii characters in my sentences in this program.
isspace as the name says check for space
you need the ctype.h header for this. or you could add in
while(c == ' ') and whie((c>='a' && c<='z') || (c >= 'A' && c<='Z')
if you don't want to use isalpace and isalnum, your choice, but it will be less elegant :)

The trouble with your code is that you consume the characters in each of your loops.
a '\n' will be consumed either by the loop that scans for words of for sentences, so the outer loop will never see it.
Here is a possible solution to your problem:
int sentences = 0;
int words = 0;
int characters = 0;
int in_word = 0; // state of our parser
int ch;
do
{
int end_word = 1; // consider a word wil end by default
ch = getch();
characters++; // count characters
switch (ch)
{
case '.':
sentences++; // any dot is considered end of a sentence and a word
break;
case ' ': // a space is the end of a word
break;
default:
in_word = 1; // any non-space non-dot char is considered part of a word
end_word = 0; // cancel word ending
}
// handle word termination
if (in_word and end_word)
{
in_word = 0;
words++;
}
} while (ch != '\n');
A general approach to these parsing problems is to write a finite-state machine that will read one character at a time and react to all the possible transitions this character can trigger.
In this example, the machine has to remember if it is currently parsing a word, so that one new word is counted only the first time a terminating space or dot is encountered.
This piece of code uses a switch for concision. You can replace it with an if...else if sequence to please your teacher :).
If your teacher forced you to use only while loops, then your teacher has done a stupid thing. The equivalent code without other conditional expressions will be heavier, less understandable and redundant.
Since some people seem to think it's important, here is one possible solution:
int sentences = 0;
int words = 0;
int characters = 0;
int in_word = 0; // state of our parser
int ch;
// read initial character
ch = getch();
// do it with only while loops
while (ch != '\n')
{
// count characters
characters++;
// count words
while (in_word)
{
in_word = 0;
words++;
}
// skip spaces
while (ch == ' ')
{
ch = -1;
}
// detect sentences
while (ch == '.')
{
sentences++;
ch = -1;
}
// detect words
while ((ch != '\n')
{
word_detected = 1;
ch = -1;
}
// read next character
ch = getch();
}
Basically you can replace if (c== xxx) ... with while (c== xxx) { c = -1; ... }, which is an artifical, contrieved way of programming.
An exercise should not promote stupid ways of doing things, IMHO.
That's why I suspect you misunderstood what the teacher asked.
Obviously if you can use while loops you can also use if statements.
Trying to do this exercise with only while loops is futile and results in something that as little or nothing to do with real parser code.

All these solutions are incorrect. The only way you can solve this is by creating an AI program that uses Natural Language Processing which is not very easy to do.
Input:
"This is a paragraph about the Turing machine. Dr. Allan Turing invented the Turing Machine. It solved a problem that has a .1% change of being solved."
Checkout OpenNLP
https://sourceforge.net/projects/opennlp/
http://opennlp.apache.org/

Related

The answer outputs blanks

Program task -
Enter a string, display it word for word on the screen.
The problem is that if you type a lot of spaces between words, they will show up when you check. How can this be fixed?
#include <stdio.h>
int main()
{
int inw = 0, i = 0, count = 0;
char s[10000];
printf("Print string (max 10000 sb):\n");
gets(s);
while (s[i] != '\0') {
if (s[i] != ' ' && s[i] != '\t') {
putchar(s[i]);
}
else if (s[i] == ' ') {
printf("\n");
}
i++;
}
return 0;
}
Ugly, but this gets the job done. Just need a flag to keep track of whether or not you just printed a new line. Also cleaned up unused variables and changed to using fgets
#include <stdio.h>
#include <stdbool.h>
int main()
{
int i = 0;
char s[10000];
bool justPrintedNewline = false;
printf("Print string (max 10000 sb):\n");
fgets(s, sizeof s, stdin);
while (s[i] != '\0') {
if (s[i] != ' ' && s[i] != '\t') {
putchar(s[i]);
justPrintedNewline = false;
}
else if (s[i] == ' ' && justPrintedNewline == false) {
printf("\n");
justPrintedNewline = true;
}
i++;
}
return 0;
}
Demo
You did a great job in the algorithm just fix a little thing.
You can create a flag and after space you increase the flag to 1.
Then you will know you will print just one space.
After printing " " check for a char that isn't " " for update the flag to 0.
When the flag is 1 DONT print anything just wait for another valid char.
Take care,
Ori
Only print a line-feeed when starting a word and after all is done.
Change code to:
If a space
-- print a '\n' when the prior character is a non-white-space.
Else
-- if (prior character is white-space) print a '\n'
-- print it
char prior = 'a';
while (s[i]) {
char ch = s[i];
if (ch != ' ' && ch != '\t') {
if (prior == ' ' || prior == '\t') {
putchar('\n');
}
putchar(ch);
}
prior = ch;
i++;
}
putchar('\n');
There is a bit of a trick to it: use a second, inside loop to skip past spaces and another to print words. The outer loop should only terminate if you have reached the end of the string.
while (s[i] != '\0')
{
// skip all spaces
while ((s[i] != '\0') && isspace( s[i] )) ++i;
// print the word
while ((s[i] != '\0') && !isspace( s[i] ))
{
putchar( s[i] );
}
// print the newline after a word
putchar( '\n' );
}
By the way, gets() is a really, really dangerous function. It should never have been included in the language. You are OK to use it for a homework, but in reality you should use fgets().
char s[1000];
fgets( s, sizeof(s), stdin );
The fgets() function is a bit more fiddly to use than gets(), but the above snippet will work for you.
Your other option for solving this homework is to use scanf() to read a word at a time from user input, and print it each time through the loop. I’ll leave that to you to look up. Don’t forget to specify your max string length in your format specifier. For example, a 100 char array would be a maximum 99-character string, so you would use "%99s" as your format specifier.

Having trouble with word function

I am having trouble with creating a function that registers input as a word. Currently it's registering words, but anything with a ' (as in don't, won't can't) is showing as 2 words. The program should take input from a file, count the words and display the word count and average letters per word.
I have played around with the code for a bit and nothing seems to solve the issue, but the following code is the closest to being correct as I have gotten.
int main(void) {
int ch, wordCount, wordAverage, letterCount;
bool inword = false;
wordCount = 0;
letterCount = 0;
while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {
letterCount++;
if (!isspace(ch) && (!inword)) {
inword = true;
wordCount++;
}
} else
inword = false;
}
wordAverage = letterCount / wordCount;
printf("The number of words was %d, and the average letters per word was %d.", wordCount, wordAverage);
}
You're using isalpha
while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {
letterCount++;
The man page states isalpha is equivalent to isupper || islower.
Try :
isalpha(ch) || ispunct(ch).

Copying input to output without unnecessary Spaces in C

I'm trying to write a program in C that copies its input to its output while replacing each string of one or more Spaces with a single Space.
My code isn't doing that but is instead taking away every second character.
This is my code:
#include <stdio.h>
main()
{
int c;
int lastc;
lastc = 0;
while(getchar() != EOF){
c = getchar();
if(c == 32 && lastc == 32)
;
else
putchar(c);
lastc = c;
}
}
Your loop should look like:
while((c = getchar()) != EOF){
if(c == 32 && lastc == 32)
;
else
putchar(c);
lastc = c;
}
In your version you get a char with getchar while checking the condition for the while loop and then as a next step you again get a char with getchar. So the first one is not used in your code. Therefore it is taking away every second character.
Keep running in while loop until you get non-space character and print just one space after you get out.
int main()
{
int c;
bool space=false;
while ((c=getchar()) != EOF) {
while (isspace(c)) {
space = true;
c = getchar();
}
if (space) {
putchar(' ');
space = false;
}
putchar(c);
}
return 0;
}
I use fgets() function to getting string from input i.e stdin and store in the scroll string.
Then you must implement a way to analyze string to find spaces in it.
When you find first space, increase index if you face another space.
This is the code.
Code
#include <stdio.h>
int main(void){
char scroll[100];// = "kang c heng junga";
fgets(scroll, 100, stdin);
printf ("Full name: %s\n", scroll);
int flag = 0;
int i=0;
while (scroll[i] != '\0')
{
if (scroll[i] == ' ' )
flag=1;//first space find
printf("%c",scroll[i]);
if (flag==0){
i++;
}else {
while(scroll[i]==' ')
i++;
flag=0;
}
}
return 0;
}
Sample input: Salam be shoma doostane aziz
Program output: Salam be shoma doostane aziz
[Edit]
Use new string st to hold space eliminated string an print as output.
Also this code work for Persian string.
char scroll[100]={0};// = "kang c heng junga";
printf("Enter a string: ");
fgets(scroll, 100, stdin);
printf ("Original string: %s\n", scroll);
char st[100]={0};
int flag = 0;
int i=0;
int j=0;
while (scroll[i] != '\0')
{
if (scroll[i] == ' ' )
flag=1;//first space find
st[j]=scroll[i];
j++;
if (flag==0){
i++;
}else {
while(scroll[i]==' ')
i++;
flag=0;
}
}
printf("Eliminate Spaces: %s", st);

How to count total number of words without using string?

I implemented this code for getting no. of words count. It works fine for all the single characters, i.e. if I type "q w r " as input, it gives me 3 words, but when I type "qwe ed df " as input, it displays 2.
#include<stdio.h>
int main()
{
int c=getchar();
int words=0;
while(c!=EOF)
{
if(c==' ' || c=='\n')
{
c=getchar();
}
else if(c>='a' && c<='z')
{
c=getchar();
if(c==' ')
{
words=words+1;
c=getchar();
}
else
{
c=getchar();
}
}
}
printf("%i\n",words);
}
The trick is to count the boundaries.
Your original code has a bug around ...
if(c==' ')
{
words=words+1;
c=getchar();
}
The code only counts words if a pair of reads results in 'a' => 'z' followed by ' '.
The trick is to simplify the loop into a read one character at a time loop. (This ensure strange boundaries don't occur), and a state machine which models whether you are eating words, or spaces.
enum Mode { word = 1, spaces = 2 };
int c=getchar();
enum Mode currentMode = spaces;
int words=0;
while(c!=EOF)
{
if(c==' ' || c=='\n')
{
if( currentMode == word ) {
words=words+1;
}
currentMode = spaces;
}
else if(c>='a' && c<='z')
{
currentMode = word;
}
c=getchar();
}
// count the last word...
if( currentMode == word ) {
words=words+1;
}
The code would now ignore any non-alpha character, as neither a word, nor a space.
EDIT : Fixed incorrect enum usage
The problem with your code is your scattered calls to getchar which are hiding the bug which you are seeing.
You need something that checks if every character is a space and if every character is a letter.
Something like:
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n') {
} else if (c >= 'a' && c <= 'z')
} else {
}
}
Now you just need to work out the logic inside those groupings. You have the logic right (if the previous letter was a character and you get a space then increment the word, otherwise just keep going). But your multiple calls to getchar means you can miss that condition.
Fill in the gaps in the above and it should work. (And then test with upper case letters!).
You are reading the characters at too many places in your while loop. This is the reason getting wrong output. Try to use this loop :
while(c!=EOF)
{
while(c == ' ' || c == '\n') //consume multiple spaces
{
c = getchar();
}
for( int i=0 ;c >= 'a' && c <= 'z'; i++) //consuming a word
{
if(i == 0) //increment word count once at the start of each word
{
words++;
}
c = getchar();
}
}
your code have a problem with strings that have
Even number of characters I think
the logic of the program isn't totally fine you have to add this condition inside the last else :
if(c==' ')
{
words=words+1;
c=getchar();
}
the program will be like this:
#include <stdio.h>
int main()
{
int c=getchar();
int words=0;
while(c!=EOF)
{
if(c==' ' || c=='\n')
{
c=getchar();
}
else if(c>='a' && c<='z')
{
c=getchar();
if(c==' ')
{
words=words+1;
c=getchar();
}
else
{
c=getchar();
if(c==' ')
{
words=words+1;
c=getchar();
}
}
}
}
printf("%d\n",words);
return 0;
}
because when you enter string of two characters and add a space, this last space will be read by the last getchar inside the else statement which don't increment the number of words.
check this results.
I hope this helps!
Every time a letter follows a space (white-space) or is the first letter, increment your word count. No need to limit count using narrow integers, file can be very long.
// Useful is...() functions declared here.
#include <ctype.h>
unsigned long long words = 0;
int previous = ' ';
int ch;
while ((ch = getchar()) != EOF) {
if (isspace(previous) && isalpha(ch)) words++;
previous = ch;
}
printf("%llu\n",words);
OP may want to adjust code to cope with digits, punctuation, etc.

c Creating a program that counts how many words are in a sentence and also capitalizes all the letters in the sentence.

So i have this code but when i run it it always says that the amount of words are 1 no matter how many i put in and hopefully it is in easy fix. I tried changing the scanf to just %s but that didn't work because it only printed out the first word but it got the number of words right.
#include <stdio.h>
int main()
{
int words = 0;
char ch,sen[100]="", i;
printf("Enter a sentence ended by a '.', a '?', or a '!':");
scanf("%[^\n]", sen);
while ((ch = getchar()) != '\n') {
if (ch == ' ')
words++;
}
words++;
for(i=0;sen[i];i++)
{
if( (sen[i]>=97) && (sen[i]<=122) )
sen[i]-=32;
}
printf("Capitalized sentence: %s\n", sen);
printf("Total number of words:%d\n", words);
return 0;
}
Your program has a major bug. scanf() won't read/store the newline. Then the newline is read by getchar(). This loop will only execute once.
while ((ch = getchar()) != '\n') {
if (ch == ' ')
words++;
}
Hence you are getting only 1 word. Why you are using 2 methods to take input.
Either use scan() and manipulate variable "sen" or use getchar() and store character 1 by 1 in sen.
// don't use scanf() in this case
i=0;
while ((ch = getchar()) != '\n') {
if (ch == ' ')
words++;
sen[i++] = ch;
}
Recommended will be to use fgets() to get such inputs. Learn about it.

Resources