43 lines
800 B
C
43 lines
800 B
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int count_words(char*);
|
|
|
|
int main(void){
|
|
|
|
char buffer[50];
|
|
int total_words = 0;
|
|
|
|
while((fgets(buffer, sizeof(buffer), stdin)) != NULL){
|
|
total_words += count_words(buffer);
|
|
}
|
|
|
|
printf("El total de palabras es: %d\n", total_words);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int count_words(char* line){
|
|
int palabras = 0;
|
|
bool hay_espacio = false;
|
|
|
|
printf("line: %s", line);
|
|
|
|
if((line[0] != ' ') && strlen(line) > 0){
|
|
palabras++;
|
|
}
|
|
|
|
for(int i = 0; i < strlen(line); i++){
|
|
if(line[i] == ' '){
|
|
hay_espacio = true;
|
|
}
|
|
|
|
if((line[i] != ' ') && (line[i] != '\n') && (hay_espacio)){
|
|
palabras++;
|
|
hay_espacio = false;
|
|
}
|
|
}
|
|
return palabras;
|
|
}
|