Thread: Words and Lines on paragraph

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    67

    Words and Lines on paragraph

    So basically I have the following task: "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.". And so far I wrote this:
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int countParagraphs(){
        int noParagraphs=0;
        int c1=getchar();
        int c2=getchar();
        if(c1!='\n'){
            c1=c2;
        }
        if(c1='\n' && c2 != EOF) // if the first character is newline and we haven't reached the end of the entire text, the number of paragraphs rises 
            noParagraphs++;
        }
        return noParagraphs;
        
    }
    int countWords(){
        int noWords=0;
        int c1=getchar();
        if(c1 != '\n'){
            if(isalpha(c1)){
            noWords++;
            }
            while(c1!=EOF){
                int c2=getchar();
                if(isspace(c1) && isalpha(c2)){ // if the first char is a space and a second is a letter => the number of words rises
                    noWords++;
                }
                c1=c2;
            }
            return noWords;
        }
        int countLines(){
            int noLines=0;
            int c1=getchar();
            while(isalnum(c1) || isspace(c1)){  //while the first char is a letter, digit or space continue reading the sequence
                int c2=getchar();
                c1=c2;
            }
            if((issalnum(c1) || issspace(c1) && c2='\n'){ // if we have a letter, digit or space before newline, increase the number of lines
               noLines++;
            }
               
            return noLines;
            
    
    
    int main(){
        
    
    
    
    
    }


    Now, to explain my thought process: I thought I would split the program into 3 functions, one to count the number of paragraphs, another to count the words on a paragraph (which I'm having trouble with) and a third counting the number of lines. I'm a newbie in C (stating the obvious), and I'm still learning the basics. I know I probably made a bunch of mistakes writing the program so far, but I'm stuck and I don't know how to continue. Also, I need help with designing the main function in order to be able to write a text, so that I can see the output based on that text. Thank you!!

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    222
    You main might be something like (in psuedo-code):

    Code:
    main function
      read first character
      while not end of file
        process character for line counting
        process character for word counting
        if it is the end of paragraph
          output line and word counts
          reset line and word counts
        read next character
    One of the more interesting ways to do this might be having "isEndOfWord(char c)", "isEndOfLine(char c)" and "isEndOfParagraph(char c)" functions.

    The isEndOfLine(c) function is easy to code - is it a New Line character?.

    The isEndOfWord(c) is a little harder - was the last character an alpha, and is this character not an alpha?

    The isEndOfParagraph(c) function is harder - has it been called with a newline, then zero or more spaces and then another newline?

    You can then use the return values from these functions to help the main program do the counting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting words, lines, and characters in a text file
    By codexer77 in forum C Programming
    Replies: 5
    Last Post: 04-21-2016, 02:40 PM
  2. Replies: 13
    Last Post: 06-07-2013, 05:45 PM
  3. Characters, words and lines counting
    By Darkobra in forum C Programming
    Replies: 4
    Last Post: 01-09-2012, 07:13 AM
  4. Replies: 11
    Last Post: 12-31-2007, 02:32 PM
  5. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM

Tags for this Thread