new to C...need to eliminate whitespace

This is a discussion on new to C...need to eliminate whitespace within the C Programming forums, part of the General Programming Boards category; hello i am new to the programming world and was wondering if anyone could help me out with a problem ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Question new to C...need to eliminate whitespace

    hello i am new to the programming world and was wondering if anyone could help me out with a problem i am having.....i need to accept keyboard input, and reproduce it after eliminating whitespace unless in "string literal"....so far i am able to print the input but im not quite sure how im supposed to delete the whitespace....here is what i have so far...

    Code:
    #include <stdio.h>
    
    int main()
    {
       int c;
       
       printf("This program eliminates whitespace from an input stream,\n");
       printf("except from within string literals in double quotes.\n");
       printf("\n");
       printf("Type characters, and enter EOF after <enter> to finish:\n");
       
       while ((c = getchar()) != EOF) {
          putchar(c);
    }
       system("pause");
       
       return 0;
    }
    i dont expect anyone to do this for me....but i have a textbook and notes and i just dont understand how to get this done....if anyone could give me any hints i would appreciate it. thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Add one bool variable, in_string_literal. Initialize it to false, and toggle it whenever you see a ".

    In the loop, if c is whitespace and in_string_literal is false, don't print it.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    im sorry but i dont understand what you are saying...i have only taken 3 classes so far in INTRO to C programming....whats is a bool? and how do i toggle? and how do i say string literal is false? im sorry im probably asking really stupid questions but i just dont understand

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    anyone??

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Wow you are given this assignment after 3 classes? What is your teacher smoking .

    That aside,

    Code:
    bool in_string_literal = false;
    a bool (http://en.wikipedia.org/wiki/Boolean_datatype) is a type (like char or int) that can only have 2 states, true and false.

    Toggle means set it to true if it's false, and set it to false if it's true.
    The easiest way to do that is with an exclusive-or
    Code:
    in_string_literal ^= 1;
    But I am guessing you haven't learned that, yet.
    http://www.cprogramming.com/tutorial...operators.html
    (it's probably a little too advanced for you at this stage, don't worry about it)

    Just use an if, then
    Code:
    if (in_string_literal) {
         in_string_literal = false;
    } else {
         in_string_literal = true;
    }
    how do i say string literal is false?
    Code:
    if (in_string_literal == false)
    or
    Code:
    if (!in_string_literal)

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    this is pure C
    why is it on the C++ board?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by vart
    this is pure C
    why is it on the C++ board?
    Indeed, so I have moved it to the C programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Just curious if you also need to figure out how to separate a string literal from a line with an embedded double-quote as in.
    => this is a line with an " embedded double-quote
    => "this is a string literal"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching for a string in txt file with whitespace!
    By invisible_wall in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2009, 10:02 PM
  2. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 01:59 PM
  4. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 08:02 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 12:55 AM

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