So I have been working on a code that will scan and print out all the spaces, words, sentences, digits, alphabets, punctuations that are in the text file. also some of the text file will have another text file saved in it to also be read in.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#define size 100
int words(char a[]);
int sentences (char a[]);
int sadp (char a[]);
int c;
char filename[size];
FILE *books, *books1, *books2, *books3;
int main(){
books = fopen("files.txt","r");
if(books == NULL){
printf("File is not found\n");
}
else{
while (fgets(filename, size-1 ,books) != NULL){
printf("%s",filename);
sentences (filename);
words(filename);
sadp (filename);
}
}
fclose(books);
return 0;
}
int sentences (char filename[]){
int count=0;
char i=0;
books3 =fopen(filename,"r");
if (books3 !=NULL){
while (fscanf(books3, "%d", &c)>0){
for(i = 0; filename[i] != '\0'; i++){
if(filename[i] == '.')
count++;
}
i++;
}
}
printf("\tSentences:\t\t%d\n", count);
return count;
}
int words(char filename[]){
char i;
int count=0;
books2 = fopen(filename,"r");
if (books2 != NULL){
while(fscanf(books2, "%d", &c)>0){
for (i = 0; filename[i] != '\0';i++){
if (filename[i]==' ' || filename[i]=='\n')
count++;
}
i++;
}
}
printf("\tWords:\t\t\t%d\n",count);
return count;
}
int sadp (char filename[]){
int alpha, digit, space, punct, i;
punct = 0; //for punctuations
alpha = 0; //alphabets
digit = 0; // digits
space = 0; // spaces between the words
i = 0; // place holder to count
books1 =fopen(filename,"r");
if (books1 != NULL){
while(fscanf(books1,"%d", &c) > 0){
if(isalpha(c)){
alpha++;
}//checks to see if it is alphabet
else if(isdigit(c)){
digit++;
}//checks to see if it is digits
else if(isspace(c)){
space++;
}//checks to see if it is space
else (ispunct(c));
punct++;
//checks to see if it is punctuations
i++;
}
}
printf("\tLetters:\t\t%d\n", alpha);
printf("\tDigits:\t\t\t%d\n", digit);
printf("\tSpaces:\t\t\t%d\n", space);
printf("\tPunctuations:\t%d\n\n", punct);
return (alpha /*digit, space, punct*/ );
}
However when I go to print this out I will get all the titles in the file to print out. but the counts for each of things that I wan to print out will not print out.
We don't need to read a file thrice to find statistics you're looking for, once is enough; going by one character at a time.
Alphabets, White-Spaces, Punctuations & Numerical-Digits
Function
int getc(FILE *stream);will read the file one character at a time. Note it returns EOF (of size more than one byte) when it reaches end-of-file.Words
Any alpha-numeric grouping separated by white space. When
isspace()is true & it comes after one/more alpha-numeric chars then we just crossed aword.Sentences
Any grouping of words terminated by sentence terminators (. ! ?). Meaning, ... !!! ??? are false positives for sentences.
Let's keep it simple for the time being. When
ispunct()is true & it(. ! ?) comes after one or more words then we found a sentence.You just have to keep flags(for word & sentence) of where you're and count words & sentences.