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).
Related
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);
Ok, just creating a basic wordcount program in C that reads from a .txt file and I'm just stuck on the actual word count function not detecting the double newlines from the file, here's the specific bit of code:
while((c = fgetc(file)) != EOF)
{
if(c == ' ')
count++;
if(c == '\n')
count++;
if (c == '\n\n')
count--;
}
printf("words = %d\n", count);
So only the first two if statements work and the third one doesn't do anything and I want that to decrement the counter as I don't want the double newline to count as a word. Does anyone know a solution? I've searched for a while but not found this specific issue.
When you encounter a double new line then \n will be read twice triggering second if on each iteration since fgetc is supposed to read only a single char at a time. As for main problem, detection of double new line can be easily achieved by introducing an extra flag. This approach should be probably extended to deal with all kinds of word separators.
int was_a_new_line = 1;
while((c = fgetc(file)) != EOF)
{
if(c == '\n')
{
if(!was_a_new_line)
{
was_a_new_line = 1;
count++;
}
}
else
{
was_a_new_line = 0;
if(c == ' ')
{
count++;
}
}
}
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.
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/
Im writing a simple program to count the number of character user is entered, and i wrote an if to check wether there is a newline but still printing it..
the code:
#include <stdio.h>
int main()
{
char ch;
int numberOfCharacters = 0;
printf("please enter a word, and ctrl + d to see the resault\n");
while ((ch = getchar()) != EOF)
{
if (numberOfCharacters != '\n')
{
numberOfCharacters++;
}
}
printf("The number of characters is %d", numberOfCharacters);
return 0;
}
what am i doing wrong?
Think about this line:
if (numberOfCharacters != '\n')
how can it make sense? You are comparing the number of characters read so far with a newline, it's like comparing apples to oranges and surely won't work. It's another variable that you should check...
Change your loop to this.
while ((ch = getchar()) != EOF)
{
if(ch != '\n')
numberOfCharacters++;
}