How to count characters, but not spaces?

This is a discussion on How to count characters, but not spaces? within the C++ Programming forums, part of the General Programming Boards category; Prelude's code with modification Code: #include <cctype> ... while ( inFile.get ( c ) ) { cout<< c; if ( ...

  1. #16
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Prelude's code with modification
    Code:
    #include <cctype>
    ...
    while ( inFile.get ( c ) ) {
      cout<< c;
      if ( c == '\n' ) {
        linecount++;
        charcount = 0;  //  it's a new line so let's start counting from zero again 
      }
      else if ( !isspace ( c ) )
        charcount++;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just count the characters in one line, when you reach \n simply output whatever message you see fit, and set charCount = 0; ready for the next line.

    If you're having trouble, post your latest code.

    Tip: Don't use this code to control a loop:
    while (!inFile.eof())
    It's a bad habbit. There's an explanation of why here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    That's a C FAQ, but the principle still stands. You can code your way out of it and still use eof(), but its better not to.

    [edit]
    Doh! Didn't see alphaoide's last message
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Yeah!!! I think I can do this. Thanks a tera-bunch for helping me out.

    Fudd's law states: "What goes in must come out." Aside from being patently untrue, Fudd's law neglects to mention that what comes out need not bear any resemblence to what went in.

    -V. Orehck III (ficticious)
    Semper Fi!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  2. consecutive number
    By stewie1986 in forum C Programming
    Replies: 3
    Last Post: 12-03-2007, 03:53 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 10:46 PM
  5. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21