Thread: How to count lines with isspace

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    How to count lines with isspace

    Hi,This time Almost, I solved question.But I have problem with counting lines.I can't figure out how to count lines with isspace.
    the code is
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    int main(void)
    {
    	char ch;
    	int space = 0, upper = 0, lower = 0, punct = 0;
    	FILE *inp;
    	inp = fopen("text.dat", "r");
    	while(!feof(inp)){
    		fscanf(inp, "%c", &ch);
    		if (isspace(ch))
    			space++;
    		if (isupper(ch))
    			upper++;
    		if (islower(ch))
    			lower++;
    		if (ispunct(ch))
    			punct++;
    		}
    	printf("The %d of text processed contained %d capital letters,\
    %d lowercase letters, and %d punctuation marks.", space, upper, lower,\
    punct);
    	return(0);
    }
    text.data file>>
    Even though she stands only 3 feet 9 inches tall, Judy Lohden has a big voice. And we're not just talking about her singing, which fills the school auditorium and wows audiences. What we love is her wry, funny narrative voice, which grabs readers and guides them through the intricate social hierarchies of cafeteria and classroom as only an outcast truly can. From the author of the hilarious memoir, Foreign Babes in Beijing, this is a novel that's filled with wit, yearning, and unforgettable character.
    output
    The 86 lines of text processed contained 9 capital letters,397 lowercase letters, and 14 punctuation marks.
    The problem is,
    it counting all whitespace, I just want to count newlines.

    How can it be with using isspace...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    From isspace(3): char classification routines - Linux man page
    Code:
    isspace()
        checks for white-space characters. In the "C""" and "POSIX""" locales, these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
    The solution? Don't use isspace(). Just check ch == '\n'
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    inp = fopen("text.dat", "r");
    while(!feof(inp)){
        fscanf(inp, "%c", &ch);
        ...
    Don't use end-of-file tests to control your loops. Instead, test the return value of the read operation for its success/failure. In this case, you are reading a character with fscanf (I'd probably use fgetc or something else) which returns the number of arguments it successfully converted. In your case, the fscanf function should return a 1 since you are reading/converting a single item (a character) each time you call the function. Therefore:
    Code:
    inp = fopen("text.dat", "r");
    while(fscanf(inp, "%c", &ch)==1){
        ...
    If you really really really want to use an end-of-file test, you can test it immediately after the read operation and then break out of the loop:
    Code:
    inp = fopen("text.dat", "r");
    while(1){
        fscanf(inp, "%c", &ch);
        if( feof(inp) ) break;
        ...
    Last edited by hk_mp5kpdw; 05-11-2011 at 08:56 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by paranoidgnu View Post
    Hi,This time Almost, I solved question.But I have problem with counting lines.I can't figure out how to count lines with isspace.

    The problem is,
    it counting all whitespace, I just want to count newlines.

    How can it be with using isspace...
    isspace() will count any "white space" in a file... including the spaces between words (hense it's name).
    What you want to do is test for newline characters... if (ch == '\n');... to know how many lines.
    As it is now you have a pretty good word counter so you might want to keep that and add a new section...

    Also if you use else if structures you might gain a bit of a performance increase...
    Code:
    while(fscanf(inp, "%c", &ch))
      {  if (ch == '\n')
           line++;
         else if (isspace(ch))
            space++;
         else if (isupper(ch))
           upper++;
         else if (islower(ch))
           lower++;
         else if (ispunct(ch))
          punct++;
         else if (isdigit(ch))
           number++;  }
    Last edited by CommonTater; 05-11-2011 at 09:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count lines in file
    By Xpload in forum C Programming
    Replies: 12
    Last Post: 12-21-2009, 06:55 AM
  2. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 07:18 PM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  5. how do I count the lines in a text file?
    By slow brain in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 02:56 PM