简体   繁体   English

删除字符串的子字符串

[英]Remove substring of string

So i was doing some coding exercice on Talentbuddy (for those who know), and i cant get why i cant finish this one. 所以我在Talentbuddy上做了一些编码练习(针对那些知道的人),我不明白为什么我不能完成这个。 The exercice is removing a substring from a string, given as input the string, the position P where beginning to remove characters and N the number of characters needed to be remove. 练习是从字符串中删除子字符串,该字符串作为输入输入,位置P开始删除字符,位置N为需要删除的字符数。

Here is what i've done : 这是我所做的:

#include <stdio.h>
#include <unistd.h>

void remove_substring(char *s, int p, int n) 
{
    int idx;

    idx = -1;
    while (s[++idx] != '\0')
       write(1, &s[idx == p - 1 ? idx + n : idx], 1);
}

When the input is "abcdefghi", P = 9 and N = 1, the result given is "abcdefgh" exactly the same as the one i get with my function. 当输入为“ abcdefghi”,P = 9且N = 1时,给出的结果为“ abcdefgh”,与我通过函数获得的结果完全相同。 But TalentBuddy keep saying me that my output is wrong and i dont thing he (talentbuddy) is wrong. 但是TalentBuddy一直对我说我的输出是错误的,我不认为他(talentbuddy)是错误的。 Maybe there is a blank space or something between the "h" and the '\\0'. 在“ h”和“ \\ 0”之间可能有空格或其他内容。 But i cant figure it cause when i add another write(1, "END", 3) at the end it appears like "abcdefghEND". 但是我无法弄清楚它的原因,当我在末尾添加另一个write(1,“ END”,3)时,它看起来像是“ abcdefghEND”。

If the question is exclusively for strings( NULL Terminated ) Why can't this be as simple as this, unless it is a homework. 如果问题只针对字符串(NULL终止),为什么不能这么简单,除非这是一项家庭作业。

void removesubstr( const char *string, const char *substring )
{
        char *p = strstr(string, substring);
        if(p)
        {
                strcpy(p,p+strlen(substring));
        }
}

Your problem is that you write something for every original index, even if it should be suppressed. 您的问题是,即使应该取消对原始索引的写入,也要为其编写一些内容。 What you write looks like abcdefgh , but it is abcdefgh<nul> , where the terminal doesn't render the <nul> . 您编写的内容看起来像abcdefgh ,但它是abcdefgh<nul> ,其中终端不呈现<nul>

You are mixing two methods here. 您在这里混合两种方法。 Either filter out the removed substring: 过滤掉删除的子字符串:

void remove_substring(char *s, int p, int n) 
{
    int i = 0;

    p--;        /* convert to C-style index */

    while (s[i] != '\0') {
       if (i < p || i >= p + n) putchar(s[i]);
       i++;
    }
}

or skip the substring by jumping over it: 或跳过子字符串来跳过它:

void remove_substring(char *s, int p, int n) 
{
    int i = 0;
    int l = strlen(s);

    while (i < l) {
        if (i + 1 == p) {
            i += n;
        } else {
            putchar(s[i++]);
        }
    }
}

You're trying to do a bit of both. 您正在尝试两者都做。

(I've avoided the awkward combination of prefix increment and starting at minus 1. And I've used putchar instead of unistd 's write. And the termination by length is so you don't inadvertently jump beyond the terminating <nul> .) (我避免了前缀增量和从负1开始的笨拙组合。并且我使用了putchar而不是unistd的write。而且按长度的终止是这样,所以您不会无意中跳到终止的<nul> 。 )

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

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