Thread: problems in removing white spaces from string of text

  1. #1
    Registered User
    Join Date
    Mar 2005

    problems in removing white spaces from string of text


    I need to write a program that shall strip all white spaces from a given string with just 1 space after each word.

    Eg. If I give the following string

    rm        asd.txt   tmp.txt                 hgfh.txt
    Note: There are training spaces in beginnign and the end...

    It should return me

    rm asd.txt tmp.txt hgfh.txt
    Also, After the tring is formatted properly, i need to pair up the 1st and the next word.

    rm asd.txt
    rm tmp.txt
    rm hgfh.txt
    Heres the code that i am using...

    #include <stdio.h>
    #include <string.h>
    int main()
      char tmp[100], cmd[100], arg[100];
      int cnt=0, spc_cnt=0, i;
      for(cnt=0; cnt < 100; cnt++)
        tmp[cnt] = NULL;
        cmd[cnt] = NULL;
        arg[cnt] = NULL;
    printf("Please enter some text... ");
    for(cnt=0; ( cnt<100 ) || (tmp[cnt] == NULL); cnt++)
            if( tmp[cnt] == ' ' )
                    for( ; (cnt<100) || (tmp[cnt] == NULL); cnt++, i++)
                            if( tmp[cnt] != ' ' )
                                    cmd[i] = tmp[cnt];
                                    cmd[i] = ' ';
                                    cnt++; i++;
      printf("TMP: %s\n", tmp);
      printf("CMD: %s\n", cmd);
      printf("ARG: %s\n", arg);
      return 0;
    Heres the output I get...

    Please enter some text... asd fghfgh rtytr
    TMP: asd fghfgh rtytr
    CMD: asd ghfgh tytr

    in CMD, second argument , f is missing... and in third argument, r is missing.

    Please help me get away from this error..

    Thank you.


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Rather than go through your code, let me tell you a simple way to do this:
    while you're not at the end of your string
        while isspace
            skip this letter
        while !isspace
            copy this character
    Naturally you need to check for reaching the end of the string in those two inner loops. But that's the very simple way to do this. There are lots of ways to do this, but that's quite a simple process. And actually, were you yourself to do this by hand, that's exactly the way you'd do it.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM