Thread: Function to replace multiple spaces from a string

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    10

    Function to replace multiple spaces from a string

    Hello,

    I need a function to replace multiple spaces with only one.
    I wrote this one but if it happens that the string starts with one or more spaces then the function will leave one there, like in this example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    void replace_spaces(char *s)
    {
        char *p = s;
        while (*s != '\0')
        {
            while (isspace(*s) && isspace(*(s + 1)))
                s++;
           *p++ = *s++;
        }
    
        *p = '\0';
    }
    
    
    int main(void)
    {
        char input[] = "        aaaa      bbbb   ";
        replace_spaces(input);
        puts(input);
        return EXIT_SUCCESS;
    }
    I'm aware of the fact that there are multiple ways to remove the first character of a string but I'd like to do this in the replace_spaces function instead of using something like memmove/strlen

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You could do it like this:
    Code:
    void replace_spaces(char *s)
    {
        char *p = s;
        char *orig = s;
    
        while (*s != '\0')
        {
            while (isspace(*s) && isspace(*(s + 1)))
                s++;
    
            if (p == orig && *s == ' ')
                s++;
    
           *p++ = *s++;
        }
     
        *p = '\0';
    }
    Last edited by GReaper; 04-23-2018 at 05:09 AM.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Use another loop to skip leading 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.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    It is interesting that the code becomes more complex if you think of this as a process with a special condition, rather than a series of processes, i.e., first, skip leading spaces and second, skip multiple spaces.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    10
    Quote Originally Posted by Salem View Post
    Use another loop to skip leading spaces.
    Quote Originally Posted by christophergray View Post
    It is interesting that the code becomes more complex if you think of this as a process with a special condition, rather than a series of processes, i.e., first, skip leading spaces and second, skip multiple spaces.
    Do you mean something like this?

    Code:
    void replace_spaces(char *s)
    {
        char *p = s;
    
    
        while(isspace(*s))
        {
            s++;
        }
        while (*s != '\0')
        {
            while (isspace(*s) && isspace(*(s + 1)))
                s++;
           *p++ = *s++;
        }
      
        *p = '\0';
    }

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by SillyStudent View Post
    Do you mean something like this?
    Code:
        while(isspace(*s))
        {
            s++;
        }
    Exactly that, yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace String Function
    By EricRoberts in forum C Programming
    Replies: 8
    Last Post: 12-28-2014, 06:40 PM
  2. Replace tabs with spaces
    By Charted in forum C Programming
    Replies: 5
    Last Post: 12-20-2014, 10:29 PM
  3. String Replace Function
    By kairozamorro in forum C# Programming
    Replies: 18
    Last Post: 08-30-2009, 01:25 PM
  4. function to find and replace a part of a string
    By 9988776655 in forum C Programming
    Replies: 2
    Last Post: 02-02-2008, 01:39 AM
  5. Using the string replace function
    By FoodDude in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2005, 02:31 PM

Tags for this Thread