Remove space in a string

This is a discussion on Remove space in a string within the C Programming forums, part of the General Programming Boards category; how can I write a program which convert a given string, it removing the leading and trailing space? Example 1: ...

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Remove space in a string

    how can I write a program which convert a given string, it removing the leading and trailing space?


    Example 1: (Let . be space)

    ......ABCD....EDE....2dkjl......kll##....

    Result should be

    ABCD....EDE....2dkjl......kll##

    Example 2: (Let . be spare)

    ...A...WW..22...oo..888....0000...u....

    Result should be

    A...WW..22...oo..888....0000...u

    My think is to use two-dimensional arrays to store the string, for ex.1

    ......
    ABCD
    ....
    EDE
    ....
    2dkjl
    ......
    kll##
    ....

    and then remove the first and end arrays.
    Can someone give me some hints on how to write the program? thank a lot.

  2. #2
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Quote Originally Posted by alice
    how can I write a program which convert a given string, it removing the leading and trailing space?


    Example 1: (Let . be space)

    ......ABCD....EDE....2dkjl......kll##....

    Result should be

    ABCD....EDE....2dkjl......kll##

    Example 2: (Let . be spare)

    ...A...WW..22...oo..888....0000...u....

    Result should be

    A...WW..22...oo..888....0000...u

    My think is to use two-dimensional arrays to store the string, for ex.1

    ......
    ABCD
    ....
    EDE
    ....
    2dkjl
    ......
    kll##
    ....

    and then remove the first and end arrays.
    Can someone give me some hints on how to write the program? thank a lot.

    I wrote a function that might be what you want:

    Code:
    #include <strings.h>
    
    char *substr(char *buf, const char *start, const char *end, char *result)
    {	
    	char *buff;
    	if ((buff = strstr(buf, start)) == NULL) 
    		return NULL;
    	while (*buff != *end && *buff != '\0')
    		*result++ = *buff++;
    	*result = '\0';
    	return result;
    }
    You could call it this way:

    Code:
    #include <stdio.h>
    
    int main()
    {
            char buf[1024], result[1024];
            FILE *file;
            if ((file = fopen("Location of file", "r")) == NULL) {
                    fprintf(stderr, "fopen: Failed\n");
                    exit(1);
            }
            if (fread(buf, 1, sizeof(buf), file) == 0) {
                    fprintf(stderr, "fread: Failed.\n");
                    exit(1);
            }
            if (substr(buf, "A", "#", result) == NULL) { //As per example one
                    fprintf(stderr, "substr: Failed.\n");
                    exit(1);
            }
            if (fclose(file) == EOF) {
                    fprintf(stderr, "fclose: Failed.\n");
                    exit(1);
            }
            fprintf(stdout, "Result = %s\n", result);
            return 0;
    }
    Last edited by loopy; 05-04-2004 at 07:02 AM.

  3. #3
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    for the example 1 & 2, the character can be in any char, ie a-z or A to Z or 1-9..
    it is only remove the leading and trailing space.

    thank.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look into the following command: isspace()

    I won't show you all the code but I'll show you this part:
    Code:
      for (first = src; isspace(*first); first++);
      for (last = first; *last != '\0'; last++);
      for (last--;isspace(*last); last--);
    Where first and last are character pointers.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    All you have to do is hook a pointer up to the beginning of the string and loop while its a space, then hook one up to the end of a string and loop backwards while its still a space, then at the last space set a \0 and copy the string into another location.
    Using the first pointer to reference it.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is incorrect:
    Code:
       if (fread(buf, 1, sizeof(buf), file) == 0) {
                     fprintf(stderr, "fread: Failed.\n");
                     exit(1);
             }
             if (substr(buf, "A", "#", result) == NULL) { //As per example one
                     fprintf(stderr, "substr: Failed.\n");
                     exit(1);
    fread() doesn't add a \0 character to the end of the array, therefore you cannot use it as a string via the str*** functions. fgets() would be better suited for reading text files.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by nonpuz
    All you have to do is hook a pointer up to the beginning of the string and loop while its a space, then hook one up to the end of a string and loop backwards while its still a space, then at the last space set a \0 and copy the string into another location.
    Using the first pointer to reference it.
    This would be problematic if the memory for this string were returned by malloc, calloc or realloc. The original address returned by the allocation function would still need to be saved for later release, and the programmer would need to remember that the pointer being used is not the pointer that must be freed. It's all too easy to forget that, even in small programs.
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
      char *mem = malloc(17);
      char *p, *end;
    
      *mem = '\0';
      strcat(mem, "   abcd  EFGH   ");
      end = mem + (strlen(mem) - 1);
      while (isspace((unsigned char)*end)) {
        --end;
      }
      *++end = '\0';
      p = mem;
      while (isspace((unsigned char)*p)) {
        ++p;
      }
      puts(p);
      free(p); /* Uh oh, should be free(mem) */
    
      return 0;
    }

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    free(p); /* Uh oh, should be free(mem) */
    When did I ever say do this? I said use the pointer to COPY it to another location, I never said use that pointer to free memory.

    I was thinking something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void killspace(char *s);
    
    int main(void)
    {
            char *s = malloc(100);
            strcpy(s,"     h a zldkjfa !!  !!    ");
            
            killspace(s);
            printf("[%s]\n",s);
    
            free(s);
            return 0;
    }
    
    void killspace(char *s)
    {
            char *b = s, *e = s+strlen(s)-1;
    
            while (*b == ' ')
                    b++;
    
            while (*e == ' ')
                    e--;
            *(e+1)='\0';
            strcpy(s,b);
    }
    Last edited by nonpuz; 05-04-2004 at 04:08 PM.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by nonpuz
    When did I ever say do this? I said use the pointer to COPY it to another location, I never said use that pointer to free memory.

    I was thinking something like this:

    <snip code>
    My appologies. I misread your post.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's a problem in this function:
    Code:
    void killspace(char *s)
    {
            char *b = s, *e = s+strlen(s)-1;
    
            while (*b == ' ')
                    b++;
    
            while (*e == ' ')
                    e--;
            *(e+1)='\0';
            strcpy(s,b);
    }
    Here you are using strcpy() with two strings that overlap, and therefore you're invoking undefined behaviour. You don't need to use any standard library functions (strlen, strcpy etc) to complete this task.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Kinda disappointed that alice hasn't had a chance to respond yet. But I feel due to the bad code floating about I should offer up my suggestion.
    As Hammer stated there is no need to use strlen(), strcpy(), or any other function. This can be solved with fairly simple pointer artimatic operations.
    Code:
    char *trim (char *str, char ch)
    {
      char *first, *last;
      int count;
    
      /* Move first to the first character that isn't the same as ch */
      for (first = str; *first == ch; first++);
      /* Move last to the null character.  Thats the only way to know 100% we are
       * removing items from the end of the string */
      for (last = first; *last != '\0'; last++);
      /* Ok now we backtrack until we find a character that isn't the same as ch */
      for (last--; *last == ch; last--);
    
      if ( first != str)
      {
        for (count=0; count< last - first + 1; count++)
          str[count] = *(first+count);
        str[count] = '\0';
      }
      else
      {
        str[last-first] = '\0';
      }
    
      return str;
    }
    Sample call would look like:
    Code:
    int main(void)
    {
      char test[] = "......ABCD....EDE....2dkjl......kll##....";
      char test2[] = "ABCDEFG......";
      char test3[] = ".......ABCDEF";
    
      puts(trim(test, '.' ));
      puts(trim(test2, '.'));
      puts(trim(test3, '.'));
    
    
      return 0;
    }

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    Here you are using strcpy() with two strings that overlap, and therefore you're invoking undefined behaviour. You don't need to use any standard library functions (strlen, strcpy etc) to complete this task.
    Interesting I did not know that...I thought I remembered reading something about it in a man page, but I tried it and it worked so i thought nothing of it.

    On a side note, isnt it faster to use strlen then loop yourself?

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How do you think strlen does it?

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    well it could be optimized or something...bleh

  15. #15
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Quote Originally Posted by nonpuz
    well it could be optimized or something...bleh
    Some string functions are written partly or maybe all in assembly, that would be faster, as far as I know.

Page 1 of 3 123 LastLast
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, 07: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, 07: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, 01:21 PM

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