从字符串中删除空格,但不在开头或结尾处删除空格

[英]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

我试图从C中的字符串中删除空格,而不是从结尾,也不是从头开始,只是字符串中的多个空格

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?

你好和每个人之间有两个空格,其中有五个空格。最终我想要从5中删除2和4中的1个空格,因此每个间隙都有1个空格。合理?

This is what I was going to do:

这就是我要做的事情:

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

    创建一个指针,将其指向元素1 char [0]的字符串。

  • do a for loop through the length of the string

    在字符串的长度上执行for循环

  • 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?

我不太清楚这里有什么好的解决方案,请记住我不会使用任何预先构建的功能。有没有人有任何想法?

5 个解决方案

#1


4  

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.

一种方法是就地进行。从头到尾循环遍历字符串。存储写指针和读指针。每个循环写指针和读指针前进一个。当你遇到一个正常的空间传输时,然后循环读取指针每次递增,直到找到一个非空格(或者显然是字符串的结尾)。不要忘记在末尾添加'\ 0',现在你有了相同的字符串,没有空格。

#2


2  

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.

如果你需要在适当的位置进行,那么创建两个指针。一个指向正在读取的字符,一个指向要复制的字符。当您遇到额外的空间时,请调整“读取”指针以指向下一个非空格字符。将读取字符指向的字符复制到写入位置。然后在复制字符后将读指针前进到字符。每当执行复制时,写指针递增1。

Example:

         write
          V
xxxx_xxxx__xxx
           ^
          Read

#3


0  

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 [],只复制你想要保留的字符。

#4


0  

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).

这是基于Goz所说的,但我认为他有手指麻烦,因为我很确定他所描述的将剥离所有空间(不仅仅是每次运行的第二个开始)。

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.

但是,一般的想法是像其他人一样使用“from”和“to”指针,但也要保留从一次迭代到下一次迭代的一些信息(状态),这样你就可以决定是否在运行空间已经或没有。

#5


0  

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

您可以执行查找并替换“”和“”,并继续执行直到找不到更多匹配项。效率低但合乎逻辑。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/17/5206b8d07979d038a7bc77acb3cb4755.html



 
  © 2014-2022 ITdaan.com 联系我们: