Remove space from input

Discussion in 'C++' started by eli m, Mar 16, 2013.

  1. eli m

    eli m Guest

    Hi, I have a program where the input involves space sometimes and i want to remove the spaces from the input. For example: if they type in hello world, then it would receive helloworld. How would i do this? Thanks in advance.
    eli m, Mar 16, 2013
    #1
    1. Advertising

  2. eli m

    Öö Tiib Guest

    On Saturday, 16 March 2013 20:53:59 UTC+2, eli m wrote:
    > Hi, I have a program where the input involves space sometimes and i want to remove the spaces
    > from the input. For example: if they type in hello world, then it would receive helloworld. How
    > would i do this? Thanks in advance.


    You 'find_first_of' space and then 'erase' it until there are none.
    Öö Tiib, Mar 16, 2013
    #2
    1. Advertising

  3. eli m

    James Kanze Guest

    On Saturday, March 16, 2013 6:53:59 PM UTC, eli m wrote:
    > Hi, I have a program where the input involves space sometimes
    > and i want to remove the spaces from the input. For example:
    > if they type in hello world, then it would receive helloworld.
    > How would i do this? Thanks in advance.


    Just use >> on strings, and then concatenate them.

    --
    James
    James Kanze, Mar 16, 2013
    #3
  4. eli m

    eli m Guest

    Just use >> on strings, and then concatenate them.
    >
    >
    >
    > --
    >
    > James

    Can you explain in more detail please?
    eli m, Mar 17, 2013
    #4
  5. eli m

    eli m Guest

    On Sunday, March 17, 2013 1:06:48 PM UTC-7, Andy Champ wrote:
    > On 17/03/2013 18:12, eli m wrote:
    >
    > > Just use >> on strings, and then concatenate them.

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> --

    >
    > >>

    >
    > >> James

    >
    > > Can you explain in more detail please?

    >
    > >

    >
    > If you read strings from your input stream using operator>> the
    >
    > whitespace between the strings is not included in the strings.
    >
    >
    >
    > If you then join the strings together you will have removed the whitespace.
    >
    >
    >
    > "abc def ghi" becomes the three strings "abc" "def" "ghi" which
    >
    > when concatenated are "abcdefghi"
    >
    >
    >
    > Andy


    Can you provide an example please? Sorry, i am a noob.
    eli m, Mar 18, 2013
    #5
  6. eli m

    James Kanze Guest

    On Sunday, 17 March 2013 18:12:57 UTC, eli m wrote:
    > Just use >> on strings, and then concatenate them.


    > Can you explain in more detail please?


    std::string word;
    while ( input >> word ) {
    output << word;
    }

    will copy the input to the output, removing all spaces. Or

    std::string results;
    std::string word;
    while ( input >> word ) {
    results += word;
    }

    will result in the input, without spaces, in results.

    --
    James
    James Kanze, Mar 18, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,855
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,620
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    975
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    702
    Michael Hudson
    Oct 24, 2003
  5. Ben C
    Replies:
    6
    Views:
    2,131
    Leif K-Brooks
    Jan 28, 2007
Loading...

Share This Page