简体   繁体   English

如何在C中使用baskspace从字符串中删除字符?

[英]How to remove a character from a string using baskspace in C?

can you give me an example of deleting characters from an array of characters in c? 你能给我一个例子,从c语言中的字符数组中删除字符吗? I tried too much, but i didn't reach to what i want 我尝试了太多,但没有达到我想要的

That is what i did: 那就是我所做的:

 int i;
 if(c<max && c>start) // c is the current index, start == 0 as the index of the start, 
                      //max is the size of the array
 {
                i = c;
      if(c == start)
                        break;

        arr[c-1] = arr[c];


 }
printf("%c",arr[c]);

A character array in C does not easily permit deleting entries. C语言中的字符数组不容易允许删除条目。 All you can do is move the data (for instance using memmove). 您所能做的就是移动数据(例如使用memmove)。 Example: 例:

char string[20] = "strring";
/* delete the duplicate r*/
int duppos=3;
memmove(string+duppos, string+duppos+1, strlen(string)-duppos);

You have an array of characters c: 您有一个字符数组c:

char c[] = "abcDELETEdefg";

You want a different array that contains only "abcdefg" (plus the null terminator). 您需要另一个仅包含“ abcdefg”(加上空终止符)的数组。 You can do this: 你可以这样做:

#define PUT_INTO 3
#define TAKE_FROM 9
int put, take;
for (put = START_CUT, take = END_CUT; c[take] != '\0'; put++, take++)
{
    c[put] = c[take];
}
c[put] = '\0';

There are more efficient ways to do this using memcpy or memmove, and it could be made more general, but this is the essence. 使用memcpy或memmove有更有效的方法可以做到这一点,并且可以使其更通用,但这是本质。 If you really care about speed, you should probably make a new array that doesn't contain the characters you don't want. 如果您真的关心速度,则可能应该制作一个不包含不需要字符的新数组。

Here's one approach. 这是一种方法。 Instead of removing characters in place and shuffling the remaining characters (which is a pain), you copy the characters you want to keep to another array: 您可以将要保留的字符复制到另一个数组中,而不是就地删除字符并改组其余字符(这很麻烦):

#include <string.h>
...
void removeSubstr(const char *src, const char *substr, char *target)
{
  /**
   * Use the strstr() library function to find the beginning of
   * the substring in src; if the substring is not present, 
   * strstr returns NULL.
   */
  char *start = strstr(src, substr);
  if (start)
  {
    /**
     * Copy characters from src up to the location of the substring
     */ 
    while (src != start) *target++ = *src++;
    /**
     * Skip over the substring
     */
    src += strlen(substr);
  }
  /**
   * Copy the remaining characters to the target, including 0 terminator
   */
  while ((*target++ = *src++))
    ; // empty loop body;
}

int main(void)
{
  char *src = "This is NOT a test";
  char *sub = "NOT ";
  char result[20];
  removeSubstr(src, sub, result);
  printf("Src: \"%s\", Substr: \"%s\", Result: \"%s\"\n", src, sub, result);
  return 0;
}

string = HELLO \\0 字符串= HELLO \\ 0

string_length = 5 (or just use strlen inside if you don't want to cache it outside this call string_length = 5(或者,如果您不想将其缓存在此调用之外,请在内部使用strlen

remove_char_at_index = 1 if you want to delete the 'E' 如果要删除'E',则remove_char_at_index = 1

copy to the 'E' position (string + 1) 复制到“ E”位置(字符串+ 1)

from the first 'L' position (string + 1 + 1) 从第一个“ L”位置开始(字符串+ 1 + 1)

4 bytes (want to get the NULL), so 5 - 1 = 4 4个字节(想要获取NULL),所以5-1 = 4

remove_character_at_location(char * string, int string_length, int remove_char_at_index) {

    /* Use memmove because the locations overlap */.

    memmove(string+remove_char_at_index, 
            string+remove_char_at_index+1, 
            string_length - remove_char_at_position);

}

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

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