Thread: How to take a string input and remove whitespace?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    How to take a string input and remove whitespace?

    I'm writing a program that will take a users postfix (aka reverse polish) and complete the calculations ,except I'm having a problem taking an input with white space between characters and removing the whitespace so I can insert the into a stack..

    How do I remove the whitespace?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well there's this:

    Code:
    scanf("%30s", buffer)
    where 31 is the size of your buffer array.

    That will fill buffer with a string without whitespace.


    You can also use strchr() to find specific white space characters in a string.
    Last edited by King Mir; 10-05-2006 at 08:31 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can't simply remove whitespace from a char array. You need to do something like:

    Code:
    char str[] = "string with some whitespace";
    char str2[100];
    int i = 0;
    char *p = str;
    while(*p)
    {
       if(*p != ' ')
          str2[i++] = *p;
       p++;
    }
    str2[i] = 0;
    Of course it may not be necessary to create a second string at all. You could parse through your array, and push any characters that are not whitespace onto your stack.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could just shift the remainder of the string to the left to cover any spaces:
    Code:
    #include <string.h>  /* For memmove(). */
    #include <ctype.h>  /* For isspace(). */
    #include <stddef.h>  /* For size_t. */
    
    void remove_whitespace(char *str) {
        char *p;
        size_t len = strlen(str);
    
        for(p = str; *p; p ++, len --) {
            while(isspace(*p)) memmove(p, p+1, len--);
        }
    }
    Note that I haven't tested that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Hi there! i recently made an RPN calculator for my stack and queue project.

    here's how i did it:

    you declare and define void push(char* s[], stack* S), char* pop(stack* S), void enqueue(char * s[], queue* Q), char * dequeue(queue* Q) first you know, among all the other functions you will need then, create stack pointer S, queue pointer Q, malloc them properly, and then: (psedocode only)

    1.) you accept user input (with whitespace) into char postfix[MAX]
    2.) you declare and initialize a string s[MAX]=" ", and another char pointer *p, then malloc it properly.
    3.) you use strtok():
    Code:
    	p=strtok(postfix,s);
                    enqueue(p, Q);
    	while((p=strtok(NULL, s)) != NULL) {/*separates the string into individual tokens*/
    		enqueue(p, Q);  /*stores the tokens as strings in the queue*/
    	}
    so the string tokens of your infix expression should be stored in the queue. after which you can then dequeue() and then do whatever you want with the string tokens.

    of course this only works if there are spaces between the tokens. if there are cases where there are no spaces, just add loop and if-else statements to separate those tokens without spaces between them.

    have fun!
    Last edited by sangken; 10-06-2006 at 10:12 PM.
    It is not who I am inside but what I do that defines me.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see how whitespace is an issue. Pull your token off the input. You're either processing "words", or "characters". If characters, simply ignore that character if it's whitespace. If words, you already know how to pull without whitespace, or you wouldn't have the concept of a "word".


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split string and remove substring
    By nyc_680 in forum C Programming
    Replies: 3
    Last Post: 03-02-2009, 04:45 AM
  2. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Writing past ...
    By Ana Val sazi in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 08:43 AM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM