Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- int main()
- {
- char str[99];
- char ch;
- int i = 0;
- int num_letter = 0;
- int num_words = 0;
- bool in_word = false;
- while((ch = getchar()) != '\n')
- {
- str[i++] = ch;
- }
- str[i] = '\0';
- while(*str)
- {
- if(*str != ' ')
- {
- num_letter++;
- if(!in_word) //if in_word = false
- {
- num_words++;
- in_word = true;
- }
- }
- else if(in_word) //if *str == ' ', check a word is finish
- {
- in_word = false;
- }
- }
- printf("number of words: %d\n", num_words);
- printf("number of letters: %d\n", num_letter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment