简体   繁体   English

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

[英]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. 最终我想要从5中删除2和4中的1个空格,因此每个间隙都有1个空格。 Make sense? 合理?

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? 有没有人有任何想法?

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',现在你有了相同的字符串,没有空格。

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

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. 你当然可以创建一个函数来返回一个删除了一个特定元素的char []。 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 [],只复制你想要保留的字符。

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”指针,但也要保留一些迭代到下一次迭代的一些信息(状态),以便你可以决定是否在运行空间已经或没有。

You could do a find and replace for " " and " " , and keep doing it until no more matches are found. 您可以对" "" "进行查找和替换,并继续执行直到找不到更多匹配项。 Innefficient, but logical. 效率低但合乎逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM