从C中的字符串中删除字符

时间:2021-05-03 00:10:53

I'm having an issue removing characters from a string. I have been able to identify the characters to remove and store them as a new string but I would like a code that enables me to remove the new string's characters from the original string (where I've labelled as xxxxxxxx).

我在从字符串中删除字符时遇到问题。我已经能够识别要删除的字符并将它们存储为新字符串,但我想要一个代码,使我能够从原始字符串中删除新字符串的字符(我将其标记为xxxxxxxx)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char input[120], to_del[120], new_input[120];
int a,b;


void removeString();

int main(void)
{
    printf("\nEnter your sentence: ");
    gets(input);

    printf("\nWhere to start: ");
    scanf("%d",&a);
    printf("\nHow many characters to delete: ");
    scanf("%d",&b);

    removeString();

    printf("\nNew sentence: %s\n",new_input);

    return ;
}

void removeString()
{
    memcpy(to_del, &input[a-1], b);
    new_input [strlen(input)-b] =xxxxxxxx;
    return ;
}

3 个解决方案

#1


EDIT in response to @SouravGhosh comment, about overlapping strings. First I copy the string to new_input and then copy the right hand part back to input to overwrite the section which must be removed.

编辑回应@SouravGhosh评论,关于重叠字符串。首先,我将字符串复制到new_input,然后将右手部分复制回输入以覆盖必须删除的部分。

int len = strlen(input);
strcpy(new_input, input);
if (a < len && a+b <= len)
    strcpy (input + a, new_input + a + b);

I notice the rest of your code doesn't check anything is happening correctly, such as the return value from scanf, and please note gets is dangerous: it is better to use fgets with stdin.

我注意到你的代码的其余部分没有检查任何正确发生的事情,例如scanf的返回值,请注意获取是危险的:最好使用带有stdin的fgets。

The strcpy instruction copies the right-hand part of the string over the part you want removed. It works by using pointer arithmetic. Adding a to input is the same as getting the address of input[a], the place where you want the removal to begin. Similary new_input[a+b] is the start of the right hand part you wish to keep.

strcpy指令将字符串的右侧部分复制到要删除的部分上。它通过使用指针算法工作。添加输入与获取输入[a]的地址相同,即输入开始的位置。类似的new_input [a + b]是您希望保留的右手部分的开头。

The point of the if condition, is that if a isn't within the string, or if the part you want removed overflows the end of the string, it can't be done.

if条件的要点是,如果a不在字符串中,或​​者如果要删除的部分溢出字符串的末尾,则无法完成。

#2


void removeString(void);

int main(void){
    printf("\nEnter your sentence: ");
    scanf("%119[^\n]%*c", input);

    printf("\nWhere to start(1 origin): ");
    scanf("%d", &a);
    printf("\nHow many characters to delete: ");
    scanf("%d", &b);

    removeString();

    printf("\nOriginal sentence: %s\n", input);
    printf("\nNew sentence: %s\n", new_input);
    printf("\nLabelled sentence: %s\n", to_del);

    return 0;
}

void removeString(void){
    strcpy(new_input, input);
    memmove(&new_input[a-1], &new_input[a -1 + b], strlen(&new_input[a -1 + b])+1);

    strcpy(to_del, input);
    memset(&to_del[a-1], 'x', b);
}

#3


You can take following approach

您可以采取以下方法

  1. copy from index 0 to a-1 of input to new_input
  2. 从索引0复制到输入到new_input的a-1

  3. copy from index a+b-1 to end of input to new_input
  4. 从索引a + b-1复制到new_input的输入结束

Finaly, your new_input will have all the values between position a and b removed.

最后,您的new_input将删除位置a和b之间的所有值。

Note:

  1. using gets() is dangerous. Use fgets() instead.
  2. 使用gets()是危险的。请改用fgets()。

  3. You cannnot assign strings using = in c. You need to copy the string containts using strcpy()/memcpy().
  4. 你不能在c中使用=来分配字符串。您需要使用strcpy()/ memcpy()复制字符串包含。

#1


EDIT in response to @SouravGhosh comment, about overlapping strings. First I copy the string to new_input and then copy the right hand part back to input to overwrite the section which must be removed.

编辑回应@SouravGhosh评论,关于重叠字符串。首先,我将字符串复制到new_input,然后将右手部分复制回输入以覆盖必须删除的部分。

int len = strlen(input);
strcpy(new_input, input);
if (a < len && a+b <= len)
    strcpy (input + a, new_input + a + b);

I notice the rest of your code doesn't check anything is happening correctly, such as the return value from scanf, and please note gets is dangerous: it is better to use fgets with stdin.

我注意到你的代码的其余部分没有检查任何正确发生的事情,例如scanf的返回值,请注意获取是危险的:最好使用带有stdin的fgets。

The strcpy instruction copies the right-hand part of the string over the part you want removed. It works by using pointer arithmetic. Adding a to input is the same as getting the address of input[a], the place where you want the removal to begin. Similary new_input[a+b] is the start of the right hand part you wish to keep.

strcpy指令将字符串的右侧部分复制到要删除的部分上。它通过使用指针算法工作。添加输入与获取输入[a]的地址相同,即输入开始的位置。类似的new_input [a + b]是您希望保留的右手部分的开头。

The point of the if condition, is that if a isn't within the string, or if the part you want removed overflows the end of the string, it can't be done.

if条件的要点是,如果a不在字符串中,或​​者如果要删除的部分溢出字符串的末尾,则无法完成。

#2


void removeString(void);

int main(void){
    printf("\nEnter your sentence: ");
    scanf("%119[^\n]%*c", input);

    printf("\nWhere to start(1 origin): ");
    scanf("%d", &a);
    printf("\nHow many characters to delete: ");
    scanf("%d", &b);

    removeString();

    printf("\nOriginal sentence: %s\n", input);
    printf("\nNew sentence: %s\n", new_input);
    printf("\nLabelled sentence: %s\n", to_del);

    return 0;
}

void removeString(void){
    strcpy(new_input, input);
    memmove(&new_input[a-1], &new_input[a -1 + b], strlen(&new_input[a -1 + b])+1);

    strcpy(to_del, input);
    memset(&to_del[a-1], 'x', b);
}

#3


You can take following approach

您可以采取以下方法

  1. copy from index 0 to a-1 of input to new_input
  2. 从索引0复制到输入到new_input的a-1

  3. copy from index a+b-1 to end of input to new_input
  4. 从索引a + b-1复制到new_input的输入结束

Finaly, your new_input will have all the values between position a and b removed.

最后,您的new_input将删除位置a和b之间的所有值。

Note:

  1. using gets() is dangerous. Use fgets() instead.
  2. 使用gets()是危险的。请改用fgets()。

  3. You cannnot assign strings using = in c. You need to copy the string containts using strcpy()/memcpy().
  4. 你不能在c中使用=来分配字符串。您需要使用strcpy()/ memcpy()复制字符串包含。