guyss i just want to ask if anyone could show me a sample program on how to count the number of words,letters,and sentences in a file....using the c language....


thanks in advance!!

Dani AI

Generated

Good instincts from @Ancient Dragon and : the main bugs are definition and method. Counting words by looking only for a space fails on tabs/newlines and multiple spaces; counting letters by "not a space" mislabels punctuation as letters; and using char for fgetc risks confusing EOF. Stream the file one character at a time, define what you want to count, and use <ctype.h> helpers.

  • Characters: every byte read.
  • Letters: alphabetic only via isalpha.
  • Words: transitions from whitespace to non-whitespace.
  • Sentences: count ., !, ? (simple heuristic).

Also, do not store the whole file in an array, and always check file opens. Avoid void main and nonstandard conio.h (as in Post #7); prefer a standard-conforming int main.

Here is a small, robust example you can compile as is:

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

int main(int argc, char *argv[]) {
    if (argc < 2) { fprintf(stderr, "usage: %s <file>\n", argv[0]); return 1; }
    FILE *fp = fopen(argv[1], "r");
    if (!fp) { perror("fopen"); return 1; }

    unsigned long long chars = 0, letters = 0, words = 0, sentences = 0;
    int in_word = 0;
    int c;
    while ((c = fgetc(fp)) != EOF) {
        chars++;
        if (isalpha((unsigned char)c)) letters++;
        if (c == '.' || c == '!' || c == '?') sentences++;
        if (!isspace((unsigned char)c)) {
            if (!in_word) { words++; in_word = 1; }
        } else {
            in_word = 0;
        }
    }
    fclose(fp);

    printf("words: %llu\nletters: %llu\nsentences: %llu\ncharacters: %llu\n",
           words, letters, sentences, chars);
    return 0;
}

Tips for testing (per ): try inputs with tabs, multiple spaces, newlines, and abbreviations like "Dr. Smith" to see how your chosen rules behave. If you need different definitions (e.g., count non-whitespace as "characters" or handle ellipses as one sentence), adjust the conditions accordingly.

Recommended Answers

All 6 Replies

Start out very simple by counting the number of letters in a sentence. When you get that finished and tested then you can write the code to count the number of words. Do just one part of it at a time so that you don't get overwhelmed by the complexity. You will need integers to contain the counts and a loop to inspect each character of the string.

Write the code to count letters, then post what you have written and feel free to ask specific questions about what you don't understand.

# include <stdio.h>
# include <string.h>
# include <ctype.h>
int static words = 0,chars = 0,senten = 0;
int i = 0;

main()/*start of main function*/
{
	char ch;
	char ch1[1000];
	FILE*fp;
	FILE*fp1;
	fp1 = fopen("NUMBERofCHars.txt","w");
	fp = fopen("Paragraph.txt","r+");
	while((ch=fgetc(fp)) != EOF) {
		ch1[i] = ch;
      	if (ch1[i] == '.'){/*condition for number of sentences*/
		senten = senten + 1;
		}
	if (ch1[i] != ' '){/*Condition for number of letters*/
		chars = chars + 1;
	}
	if (ch1[i] == ' '){/*condition for number of words*/
		words  = words + 1;
		}
	i++;	
	}
	fprintf(fp1,"Number of words : %d\n",words);
	fprintf(fp1,"Number of letters: %d\n",chars);
	fprintf(fp1,"Number of sentence: %d\n",senten);
	fclose(fp);	fclose(fp1);
}/*End of main*/

here is my code..

i get an on error when i run the program it does not give the exact number of words letters and sentence....i don't know where i had a mistake on my code..

Gee, I wonder what you types in. I also wonder what answers you got.

Any help is dependent on the input and output.

You don't need that chr1 declared on line 10. Why save the input characters? Just read a character from the file and process it.

line 17: sentences also end in ? and ! characters.

line 20: you need to increment the number of characters count for each character read in from the file, not just the number of spaces. Delete lines 20 and 22, leaving only chars = chars + 1.

Thanks alot.........

#include<stdio.h>
#include<conio.h>
void main(){
 printf("THANK U!");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.