Thread: Interpreting whitespace characters from standard input

  1. #1
    Registered User
    Join Date
    Oct 2012

    Interpreting whitespace characters from standard input

    I'm relatively new to C programming, and I have a pretty trivial question.

    I'm trying to convert all whitespace characters from standard in to a single space. I understand that my code won't work as '\n' will be read character by character and not as a whole.

    How might I change this to interpret '\n' and others like '\t' as a single whitespace character?


    int main(){
        int c;
        c = getchar();
        while(c != EOF){
            c = ' ';
        c = getchar();
        return 0;

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Consider using a flag to keep track of whether you're in a sequence of white space or not. Don't print anything until you are out of the sequence.

    If that's not enough of a hint, it helps with problems like this to work through some examples by hand, with paper and pencil. Pick several sample strings. Go through them character by character, and convert each one to a string where all white space is compressed into one single space char. Pay careful attention to all the little steps and details you do. Write them down in plain English. Translate that to pseudo code and finally into real code, writing small chunks (5-10 lines max) and testing often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to read in whitespace characters
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 05-22-2012, 11:15 AM
  2. help showing amount of whitespace and characters
    By mattz714 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2010, 03:13 PM
  3. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 10:24 PM
  4. counting whitespace characters
    By artec_19 in forum C Programming
    Replies: 1
    Last Post: 10-30-2003, 02:50 PM
  5. Interpreting characters from physical paper
    By Zewu in forum Tech Board
    Replies: 10
    Last Post: 12-31-2002, 03:53 AM