Trim whitespaces

This is a discussion on Trim whitespaces within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a C program to trim whitespaces before the string. But, it isn't working properly. ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    9

    Trim whitespaces

    Hi,

    I am trying to write a C program to trim whitespaces before the string. But, it isn't working properly. Following is what I tried

    char *lefttrim(char *str)
    {
    int i=0;
    while(strlen(str) > 0 && str[0] == ' ')
    {
    for(i = 1; i< strlen(str); i++)
    {
    str[i-1] = str[i];
    str[strlen(str)] = '\0';
    }
    return(str);
    }

    It would be great if someone can point out some mistake.

    Thanks,
    Pingoo

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    str is a local variable to your function you need to dereference it for it to have effect outside the function. Returning it is bad since it will be popped of the stack once the function returns.

    How are you using the function?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    I am taking cmd line arguments and calling this function to trim the whitespaces.

    int main(int argc, char **argv)
    {
    char str[100];
    int i;

    for(i=1; i<=strlen(str); i++)
    {
    lefttrim(argv[i]);
    printf("The value of i is %d\n", i);
    printf("Trimed string is %s\n",argv[i]);
    }
    exit(0);

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    << !! Posting Code? Read this First !! >>
    Read this before posting any more code.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Actually forget what I said before, you are dereferencing it. But you need to change the index where your terminating zero is placed. Although returning a pointer to the string seems pointless since you don't use it and it's actually pointing the the base of the string you are sending the function.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Pingoo View Post
    I am taking cmd line arguments and calling this function to trim the whitespaces.

    int main(int argc, char **argv)
    {
    char str[100];
    int i;

    for(i=1; i<=strlen(str); i++)
    {
    lefttrim(argv[i]);
    printf("The value of i is %d\n", i);
    printf("Trimed string is %s\n",argv[i]);
    }
    exit(0);
    And what do you expect str to contain in this example? Also, what does str have to do with argv[i]?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Also it's helpful if you can describe the problem beyond "it's not working". In this case I think it would be nice if you could provide, what the program does, and how that differs from what you are expecting.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    char *lefttrim(char *str)
    {
        int i=0;
        while(strlen(str) > 0 && str[0] == ' ')
        {
            for(i = 1; i< strlen(str); i++)
            {
                str[i-1] = str[i];
                str[strlen(str)] = '\0';
            }
            return(str);
        }
    Do you see error now?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    After doing the required changes, I am getting following output

    $ gcc -o trim trim_whitespace.c
    $ ./trim This is a test string.
    The value of i is 1
    Trimed string is This
    The value of i is 2
    Trimed string is is
    The value of i is 3
    Trimed string is a
    The value of i is 4
    Trimed string is test
    The value of i is 5
    Trimed string is string.
    Segmentation fault
    ---------------------------------------
    I don't know why it is saying Segmantation fault.
    Last edited by Pingoo; 06-27-2010 at 02:18 PM.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What condition are you using instead of strlen(str) as claudiu pointed out in your for loop?

    As you can see, the input is 5 strings. The segmentation fault happens after string 5, it appears as you are doing operations on argv[6].

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like you should have access to "gdb" for debugging if you are on linux, which makes it easy to find seg faults. Have a look here, it will take you ~5 min:

    getting a segfault using pointers
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Worth noting as well is that your test strings doesn't contains any space, so you aren't really testing your function. You would need to enter them like this:

    Code:
    ./trim "  this" "   is" "   a" "  test."

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    Thanks for correcting me. It is working fine now.

    gdb was useful too.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Good to hear that. What is your current code? I noticed that you used strlen() in a loop, but you actually do not need to do that.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whitespaces
    By happyclown in forum C Programming
    Replies: 2
    Last Post: 01-06-2009, 09:33 PM
  2. Remove whitespaces from char*
    By desmond5 in forum C Programming
    Replies: 17
    Last Post: 03-10-2008, 11:39 AM
  3. sscanf & whitespaces
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 09:06 AM
  4. trim string function (code)
    By ipe in forum C Programming
    Replies: 9
    Last Post: 01-05-2003, 11:28 PM
  5. Extracting Whitespaces
    By TechWins in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 07:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21