C代码删除字符串中不区分大小写的子串时遇到的问题

bmvo0sr5  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(130)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int erase_once(char *input_string, const char *substring, int case_sensitive) {
    size_t string_length = strlen(input_string);
    size_t substring_length = strlen(substring);

    if (string_length < substring_length || substring_length == 0) return 0;

    size_t i;
    for (i = 0; i < string_length - substring_length + 1; i++) {
        int match_found = 1;

        for (size_t j = i; j < i + substring_length; j++) {
            if ((case_sensitive == 0 &&
                 tolower(input_string[j]) != tolower(substring[j-i])) ||
                input_string[j] != substring[j-i]) {
                match_found = 0;
                break;
            }
        }

        if (!match_found) continue;

        for (; i < string_length - substring_length; i++) {
            input_string[i] = input_string[i + substring_length];
        }
        input_string[i] = '\0';

        return 1;
    }

    return 0;
}

int erase(char *input_string, const char *substring, int case_sensitive) {
    int count;
    for (count = 0; erase_once(input_string, substring, case_sensitive); count++);
    return count;
}

int main(void) {
    char main_string[] = "abcdef cde AbCDE";
    const char *substring_to_erase = "abc";

    erase(main_string, substring_to_erase, 0);
    printf("%s\n", main_string);

    return 0;
}

字符串
我尝试通过设置变量int case_sensitive来使其区分大小写;当变量为0时,它不敏感,当它为1时,它区分大小写,因此例如:对于输入“abcdef cde AbCDE”和子字符串“abc”,如果它为1,则应返回-“def cde AbCDE”;对于相同的input和case_sensitive设置为0,它应该返回“def cde DE”另一个例子是对于字符串xYXyXYXY和子字符串xy和sensitive设置为0,它应该返回空字符串
但是没有,你能帮我找出错误吗?

xcitsw88

xcitsw881#

你刚刚忘记检查是否case_sensitive==1,其他明智的它将总是你的'if条件'的后半部分将被检查和考虑.这是你需要编辑的行

if ((case_sensitive == 0 &&
                 tolower(input_string[j]) != tolower(substring[j-i])) ||
                (case_sensitive == 1 &&
                input_string[j] != substring[j-i]))

字符串
注:如果你的解决方案过于复杂,而且你的代码不够干净,这些技巧将帮助你识别任何问题,并使你易于调试

jobtbby3

jobtbby32#

将任务分解为更小的部分

int strneq(const char *s1, const char *s2, size_t n, int cs)
{
    char c1 = 0, c2 = 0;
    while(n-- && *s1 && *s2)
    {
        if(cs)
        {
            c1 = *s1;
            c2 = *s2;
        }
        else
        {
            c1 = tolower(*s1);
            c2 = tolower(*s2);
        }
        if(c1 != c2) break;
        s1++;
        s2++;
    }
    return !(c1 == c2);
}

char *mystrstr(const char *s, const char *find, int cs)
{
    size_t slen = strlen(s);
    size_t flen = strlen(find);
    char *result = NULL;
    if(slen >= flen)
    for(size_t index = 0; index <= slen - flen; index++)
    {
        if(!strneq(s + index, find, slen - index  + 1, cs))
        {
            result = (char *)(s + index);
            break;
        }
    }
    return result;
}

char *strrem(char *haystack, const char *needle, int cs)
{
    size_t nlen = strlen(needle);
    size_t hlen = strlen(haystack);
    char *current;
    while((current = mystrstr(haystack, needle, cs)))
    {
        while((*current = *(current + nlen))) current++;
    }
    return haystack;
}

int main(void)
{
    char h[] = "abcdef cde AbCDE";
    char *n = "abc";

    printf("'%s'\n", strrem(h,n,0));
}

字符串
https://godbolt.org/z/M7bh8Pq7a

3yhwsihp

3yhwsihp3#

1函数内的所有内容:

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

int erase(char* input_string, const char* substring, int case_sensitive) {
    int len1 = strlen(input_string),
        len2 = strlen(substring);

    if (len1 < len2 || len1 < 0) return 0;

    int eraseCount = 0,
        N = len1 - len2 + 1;
    
    for (int i = 0; i < N;) {
        int found = 1;
        
        for (int j = 0; j < len2; j++) {
            char c1 = input_string[i + j], c2 = substring[j];
            
            if(( case_sensitive && tolower(c1) != tolower(c2)) || //case_sensitive == 0 - false, otherwise true (any value)
               (!case_sensitive && c1 != c2)) {
                    found = 0;
                    i += j;
                    break;
            } 
        }
        
        if(found) {
            eraseCount++;
            
            for(int j = i; j < N; j++)
                input_string[j] = input_string[j + len2]; //move left
            for(int j = N + len2 - 2; j >= N - 1; j--)
                input_string[j] = 0; //removed
            
            N -= len2;
        } else i++;
    }
    
    return eraseCount;
}

void test1(){
    char main_string[] = "abcdef cde AbCDE";
    const char *substring_to_erase = "abc";
    erase(main_string, substring_to_erase, 0);
    printf("String: \"%s\"\n", main_string);
}

void test2(){
    char main_string[] = "xYXyXYXY";
    const char *substring_to_erase = "xy";
    erase(main_string, substring_to_erase, 1);
    printf("String: \"%s\"\n", main_string);
}

int main(void) {
    test1();
    test2();
    return 0;
}

字符串

结果:

String: "def cde AbCDE"
String: ""

...Program finished with exit code 0
Press ENTER to exit console.

相关问题