Thread: Remove space in a string

  1. #31
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    I want to change the function prototype to

    Code:
    int trimString(char* str);
    am my code correct? thk a lot.
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int trimString(char* str);
    {
      char *str = malloc(30);
      char *p, *end;
    
      *str = '\0';
      strcat(str, "   abcd  EFGH   ");
      end = str + (strlen(str) - 1);
      while (isspace((unsigned char)*end)) {
        --end;
      }
      *++end = '\0';
      p = str;
      while (isspace((unsigned char)*p)) {
        ++p;
      }
      puts(p);
      free(p); 
    
      return 0;
    }

  2. #32
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well
    your function parameter and local variable share the same name, so expect lots of fun there.
    There's a spare ; at the end of your function definition
    Why malloc only 30 ?
    Why the casts in isspace
    Those while loops will blow up if you try and trim a string containing only spaces
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #33
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    oh I forgot to remove ; after the function definition.
    alloc 30 just for testing...it's doesnt matter the length.

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a fun one:
    Code:
        for( head=totrim, tail=totrim+(totrim?strlen(totrim)-1:0);
             head&&head!=tail;
             (isspace(*head)?head++:0),(isspace(*tail)?tail--:0) )
                    if( !isspace( *head ) && !isspace( *tail ) )
                    {
                            *++tail='\0';
                            break;
                    }
            printf("\'%s\'\n", head ? head : "" );
    [edit]
    Hm... There's a very minor bug. Do you know what it is? Here's a hint, it's easily fixed, and will save one character of typing in fixing it. It will only occur in one specific case.
    [/edit]

    Quzah.
    Last edited by quzah; 05-16-2004 at 08:25 AM.
    Hope is the first step on the road to disappointment.

  5. #35
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by alice
    I want to change the function prototype to

    Code:
    int trimString(char* str);
    am my code correct? thk a lot.
    No
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int trimString(char* str);
    {
      char *str = malloc(30);   // redefinition of str.  Try the statement below
      char *p, *end;
      str = malloc(30); // this is in the proper place 
    
      *str = '\0';   // Why?  Just use strcpy() instead 
      strcat(str, "   abcd  EFGH   ");
      end = str + (strlen(str) - 1);
      while (isspace((unsigned char)*end)) {
        --end;
      }
      *++end = '\0';
      p = str;
      while (isspace((unsigned char)*p)) {
        ++p;
      }
      puts(p);
      free(p);    // Confusing and wrong.  You malloc'd str so release str 
    
      return 0;
    }
    [edit]
    Oh, and why are you passing in the pointer to str and then throwing away everything it points to? For testing wouldn't it be better to do:
    Code:
    int main()
    {
        char *str;
    
        str = malloc(30);
        strcpy(str, "   abcd  EFGH   ");
        int trimString(str);
        free(str);
        return 0;
    }
    
    int trimString(char* str);
    {
        char *end, p;
    
        end = str + (strlen(str) - 1);
        while (isspace((unsigned char)*end)) 
        {
            --end;
        }
        *++end = '\0';
        p = str;
        while (isspace((unsigned char)*p)) 
        {
            ++p;
        }
        puts(p);
        return 0;
    }
    The fact that you are changing str can be dealt with next, when you pass back the new string instead of outputting it in the function.
    [/edit]
    Last edited by WaltP; 05-16-2004 at 11:41 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #36
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    My compiler give error "Declaration is not allowed here"
    on the line ''int trimString(str);''

  7. #37
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > My compiler give error "Declaration is not allowed here"
    > on the line ''int trimString(str);''
    Yeah, see what I said 2 days ago
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #38
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int trimString(char* str);
    {
    That's because there's one tiny character extra...*

    On an aside, I see no one spotted the bug in my code. It has to do with !=. The bug will show itself if you pass the function " " (two white spaces) as your string. That's the only time it will happen. Like I said, very small, extremely rare occurance, but there none the less. Chaning it to a less-than fixes solves the problem.

    Quzah.
    *[edit]Curses, foiled again![/edit]
    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. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM