views:

175

answers:

5

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 2 spaces between hello and everyone, and five spaces from this to is. Ultimatley i would want to remove 1 space from the 2 and 4 from the 5, so every gap has 1 space exactly. Make sence?

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 wont be using any pre built functions. Does anyone have any ideas.

Please dont provide any solutions as i am doing this for homework, i just cant see what to do when i no there is a space after another.

thanks

wow - somne really good answers for me to get cracking with - thanks for the help guys

i am sorry that i have not picked a correct answer - which one i pick is a little hard to choose as they all make sence. maybe a mod could do it dependant on what they think.

+4  A: 

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.

Goz
A: 

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.

Thirler
+2  A: 

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
kgiannakakis
wow - very informative, thanks very much
leo
Your method (The same as mine) works just as well in-place ya realise :)
Goz
really, so i was almost there then? :D - cheers Goz
leo
A: 

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.

Steve314
Did you read "Please dont provide any solutions as i am doing this for homework"?
anon
dont worry i shall not be using it anyway, i like the idea of the read write idea above, not sure if this used that, anwyays it doesnt matter now as its gone ;)
leo
@leo - Goz and kgiannakakis have definitely given a good starting point. Neil is right, though, and I've written enough similar complaints that I'm very red-faced right now :-(
Steve314
A: 

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

Eric