I am trying to remove spaces from a string in C, not from the end, nor the beginning, just multiple spaces in a string
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.
創建一個指針,將其指向元素1 char 的字符串。
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]的指針是一個空格而我的指針在元素[i + 1]空間然后做某事
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.
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.
這里的一個難點是你無法輕易地從字符數組中刪除元素。你當然可以創建一個函數來返回一個刪除了一個特定元素的char 。另一個選擇是創建一個額外的數組,指示你應該保留哪些字符,然后再過一次char ,只復制你想要保留的字符。
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.
編輯 - 哎呀 - 關於Goz的錯誤,雖然“額外的”措辭只能正確地覆蓋兩個空格的運行。
EDIT - oops - pre-written solution removed...
編輯 - oops - 刪除預先寫好的解決方案......
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.