65 lines
1.1 KiB
C
65 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define SIZE_LINE 1000+1
|
|
|
|
bool isAllUpper(const char * str);
|
|
int countUpperWord(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 + countUpperWord(str);
|
|
}
|
|
|
|
printf("Total all-uppercase words: %d\n", countWord);
|
|
|
|
fclose(file);
|
|
} else {
|
|
perror("Error (source)");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool isAllUpper(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 countUpperWord(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 (isAllUpper(pc)) {
|
|
countWord++;
|
|
}
|
|
}
|
|
|
|
return countWord;
|
|
} |