简体   繁体   中英

Remove spaces from a string, but not at the beginning or end

I am trying to remove spaces from a string in C, not from the end, nor the beginning, just multiple spaces in a string

For example

hello  everyone this     is a test

has two spaces between hello and everyone, and five spaces from this to is. Ultimately I would want to remove 1 space from the 2 and 4 from the 5, so every gap has 1 space exactly. Make sense?

This is what I was going to do:

  • create a pointer, point it to the string at element 1 char[0].

  • do a for loop through the length of the string

  • then my logic is, if my pointer at [i] is a space and my pointer at element [i+1] space then to do something

I am not quite sure what would be a good solution from here, bearing in mind I won't be using any pre-built functions. Does anyone have any ideas?

One way is to do it in-place. Loop through the string from the beginning to end. store a write pointer and a read pointer. Each loop the write pointer and read pointer advances by one. When you encounter a space transfer it as normal but then loop the read pointer incrementing each time until a non-space is found (Or the end of the string, obviously). Don't forget to add a '\\0' at the end and you now have the same string without the spaces.

Are you allowed to use extra memory to create a duplicate of the string or you need to do the processing in place?

The easiest will be to allocate memory equally to the size of the original string and copy all characters there. If you meet an extra space, do not copy it.

If you need to do it in place, then create two pointers. One pointing to the character being read and one to the character being copied. When you meet an extra space, then adapt the 'read' pointer to point to the next non space character. Copy to the write position the character pointed by the read character. Then advance the read pointer to the character after the character being copied. The write pointer is incremented by one, whenever a copy is performed.

Example:

         write
          V
xxxx_xxxx__xxx
           ^
          Read

A hard part here is that you can not remove an element from the array of characters easily. You could of course make a function that returns a char[] that has one particular element removed. Another option is to make an extra array that indicates which characters you should keep and afterward go over the char[] one more time only copying the characters you want to keep.

This is based on what Goz said, but I think he had finger trouble, because I'm pretty sure what he described would strip out all spaces (not just the second onwards of each run).

EDIT - oops - wrong about Goz, though the "extra one" wording would only cover runs of two spaces correctly.

EDIT - oops - pre-written solution removed...

The general idea, though, is to use the "from" and "to" pointers as others did, but also to preserve some information (state) from one iteration to the next so that you can decide whether you're in a run of spaces already or not.

You could do a find and replace for " " and " " , and keep doing it until no more matches are found. Innefficient, but logical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM