School-Coding-Cpp/sfusi/countLowerWord_textfile.cpp

66 lines
1.1 KiB
C++

#include <iostream>
#include <cstring>
#include <cctype>
#define SIZE_LINE 1000+1
using namespace std;
bool isAllLower(const char * str);
int countLowerWord(const char * str);
int main(void) {
FILE * file = fopen("parole.txt", "rt");
if (file != NULL) {
char str[SIZE_LINE];
int countWord = 0;
while (fgets(str, sizeof(str), file)) {
countWord = countWord + countLowerWord(str);
}
cout << "Total all-lowercase words: " << countWord << endl;
fclose(file);
} else {
perror("Error (source)");
}
return 0;
}
bool isAllLower(const char * str) {
int countAplha = 0;
if (strlen(str) == 0) {
return false;
}
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
countAplha++;
if (isupper(str[i])) {
return false;
}
}
}
return true;
}
int countLowerWord(const char * str) {
char buffer[SIZE_LINE];
int countWord = 0;
strcpy(buffer, str);
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
if (isAllLower(pc)) {
countWord++;
}
}
return countWord;
}