【问题标题】:Removing characters from an input string in c从c中的输入字符串中删除字符
【发布时间】:2015-05-04 16:06:52
【问题描述】:

我的目标是从 C 中的字符串中删除用户定义数量的字符。

代码要求用户输入一个字符串,指出他们想要删除的字符的开头,并指出他们想要从该位置删除多少个字符,然后代码显示结果。

我希望有人能想出一个代码来执行所需的功能并提供分步信息,因为我昨天才开始编码

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


 int a,b;
 char text[20];
 char new_sentence[20];
 int how_much;

 void removeString(char* text, int b, int how_much);
 int main(void)

 {


  printf("\nEnter your sentence: ");
  gets(text);
  printf("\nWhere to start: ");
  scanf("%d",&a);
  printf("\nHow many characters do you want to remove: ");
  scanf("%d",&how_much);

  removeString(char* text, int b, int how_much);

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

  return 0;
  }


  void removeString(char* text, int b, int how_much)
   {
     how_much = b - a;

     memmove(&text[a], &text[b],how_much);

     }

【问题讨论】:

    标签: c string memmove


    【解决方案1】:

    我不会为你编写代码,因为你提供的示例代码确实是一个最小的例子,但这些是你应该做的事情:

    1. removeString() 函数签名(及其相应的主体)更改为removeString(char* your_string, int where_to_start, int how_much_to_cut)。请注意,在 C 语言中,char[] 等效于 char*,在您的情况下,最好将 char* 传递给您的函数,因为您将拥有可变长度的字符串。
    2. 使用strlen() 函数计算传递给removeString() 函数的字符串(即char*)的长度。您可以根据以这种方式计算的长度对参数进行错误检查。
    3. 请注意,您可能需要在字符串中间剪切一些字符 - 因此,我将计算结果字符串的长度以及 realloc() char* your_stringmalloc()新字符串并从removeString() 函数返回。
    4. 您可以通过this webpage 上的信息将输入参数(字符串、剪切位置、剪切多少字符)添加到您的程序中。

    编辑:

    您可以在下面查看问题的解决方案:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LEN 20
    
    char* removeString(char* text, int where_to_start, int how_much)
    {
        // You should consider all the special (invalid) cases here
        // I'm demonstrating how to handle common case
        size_t len = strlen(text);
        char* new_string = (char*) malloc(sizeof(char) * len - how_much);
        memcpy(new_string, text, where_to_start);
        memcpy(new_string+where_to_start, text+where_to_start+how_much, len-where_to_start-how_much);
        return new_string;
    }
    
    int main(void)
    {
        int a,b;
        char buffer[MAX_LEN];
    
        printf("Enter your sentence:\n");
        scanf("%s", buffer);
    
        printf("Where to start:\n");
        scanf("%d",&a);
        printf("How many characters do you want to remove:\n");
    
        scanf("%d",&b);
    
        char* removed = removeString(buffer, a, b);
    
        printf("New sentence: %s\n", removed);
    
        free(removed);
        return 0;
    }
    

    【讨论】:

    • 我知道我提供了一个最小的示例,因此我编辑了帖子以展示我在过去几分钟内想出的内容。在我尝试使用您提供的说明时,欢迎进行任何具体的更改
    • 我觉得很好,现在尝试将参数传递给removeString()
    • 成功了。不幸的是,我所做的只是从 memove 函数中的 [&a] 和 [&b] 中删除了 &,所以我什至没有开始使用您的解决方案,但是如果您告诉我如何更改第三个论点是“他们想要剪切多少”而不是“最终要剪切的角色”
    • 我看到你添加了最后一个字符作为输入参数。我仍然认为您应该添加一些输入验证并考虑到我提到的特殊情况。至于你的问题,为什么不直接how_much = final_char_pos - start_char_pos
    • 不工作。 how_much = text[b] - text[a] 我假设。我已经为 how_much 创建了一个整数变量,我已经更改了 scanf 函数以提供 how_much 并将算术包含在 removestring 函数中。对不起,但你需要对我说清楚,因为我不知道我在做什么
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2023-02-06
    相关资源
    最近更新 更多