從字符串中刪除空格,但不在開頭或結尾處刪除空格

[英]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 联系我们: