Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

pointers

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?

like image 214
leo Avatar asked Feb 17 '10 10:02

leo


People also ask

How do you exclude a space in a string?

replace() We can use replace() to remove all the whitespaces from the string. This function will remove whitespaces between words too.

How can you trim whitespace characters from the beginning or end of a string?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.

How do you return the string without any whitespace at the beginning or the end?

You can call the trim() method on your string to remove whitespace from the beginning and end of it. It returns a new string.


2 Answers

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.

like image 188
Goz Avatar answered Nov 02 '22 22:11

Goz


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
like image 30
kgiannakakis Avatar answered Nov 02 '22 20:11

kgiannakakis