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.