Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- /* Paragraph info:
- * Write a program that reads all input and prints for each paragraph
- * the number of words and lines. A paragraph is a portion of text
- * that does not start with a newline, ends with a single newline or
- * EOF and is separated by at least one newline from other paragraphs.
- */
- void paragraphInfo(){
- char a, b;
- unsigned wordCount = 0, lineCount = 0;
- int spaceBeforeWord = 0;
- a = getchar();
- while((b = getchar()) != EOF){
- if(isspace(a) && !isspace(b)){
- spaceBeforeWord = 1;
- }
- if(isspace(b) && !isspace(a) && spaceBeforeWord){
- wordCount++;
- }
- if(b == '\n' && a != '\n'){
- lineCount++;
- }
- if(b == '\n' && a == '\n'){
- printf("words: %u, lines: %u\n", wordCount, lineCount);
- wordCount = 0;
- lineCount = 0;
- }
- a = b;
- }
- }
- int main(){
- paragraphInfo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement