How to delete the space characters from the string

Discussion in 'C Programming' started by jayapal, Nov 20, 2007.

  1. jayapal

    jayapal Guest

    Hi all,

    Is there any library function in order to delete the space characters
    from the given stirng.

    Thanks,
    Jay
    jayapal, Nov 20, 2007
    #1
    1. Advertising

  2. jayapal said:

    > Hi all,
    >
    > Is there any library function in order to delete the space characters
    > from the given stirng.


    No, but see K&R2, page 47.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 20, 2007
    #2
    1. Advertising

  3. jayapal

    santosh Guest

    In article
    <>,
    jayapal <> wrote on Tuesday 20 Nov 2007 6:03 pm:

    > Hi all,
    >
    > Is there any library function in order to delete the space characters
    > from the given stirng.


    Not in Standard C, but it's pretty easy to roll your own. If you are
    having problems doing so, then by all means post your code along with
    your question.

    PS. If you do "roll your own" you might want to make the function more
    generic by designing it to remove all occurrences of a user specified
    character, rather than creating it for the specific case of a space
    character alone.
    santosh, Nov 20, 2007
    #3
  4. jayapal

    Mark Bluemel Guest

    jayapal wrote:
    > Hi all,
    >
    > Is there any library function in order to delete the space characters
    > from the given stirng.


    How about looking in the standard?

    The answer is "no", anyway. But it wouldn't be difficult to write one,
    would it?

    Untested code follows:-

    /* "squeeze a string "in place" */
    char *removeSpaces(char * string) {
    char *source = string;
    char *target = string;

    while(*source) {
    if (*source != ' ') {
    *target++ = *source;
    }
    source++;
    }
    *target = '\0';
    return string;
    }
    Mark Bluemel, Nov 20, 2007
    #4
  5. jayapal

    Richard Guest

    Mark Bluemel <> writes:

    > jayapal wrote:
    >> Hi all,
    >>
    >> Is there any library function in order to delete the space characters
    >> from the given stirng.

    >
    > How about looking in the standard?


    So we move from "RTFM" to "why don't you look in the standard". Despite
    the standard (standard, not the draft )not being available for free
    online and almost inaccessible to anyone without formal CS training.

    >
    > The answer is "no", anyway. But it wouldn't be difficult to write one,
    > would it?


    Possibly for a C noob...

    >
    > Untested code follows:-
    >
    > /* "squeeze a string "in place" */
    > char *removeSpaces(char * string) {
    > char *source = string;
    > char *target = string;
    >


    /*alternatively and as untested something like the following which uses
    natural termination like strcpy.*/

    char c;
    while(*target = *source++)
    if(*target)!=' ')
    target++;

    /*
    > while(*source) {
    > if (*source != ' ') {
    > *target++ = *source;
    > }
    > source++;
    > }
    > *target = '\0';

    /*
    > return string;


    > }
    Richard, Nov 20, 2007
    #5
    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,874
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,632
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    989
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    711
    Michael Hudson
    Oct 24, 2003
  5. Bazsl
    Replies:
    11
    Views:
    258
    James Edward Gray II
    Oct 12, 2007
Loading...

Share This Page