从另一个字符串中删除每个字符串| C

从另一个字符串中删除每个字符串| C,c,C,我正试图编写一个代码,从另一个字符串中删除一个字符串的所有匹配项。如果我发现某个事件,则要求我传递1;如果我没有发现,则要求传递0。我尝试使用两个循环来迭代输入字符串和需要删除的子字符串。如果两个字符串在某个索引上不相等,我们将继续迭代。如果它们相等,则尝试“编辑”结果字符串,但没有成功。 我将感谢任何形式的帮助! 这是我的代码:已编辑 #include <stdio.h> #define STRING_SIZE 100 int str_remover(char result_st

我正试图编写一个代码,从另一个字符串中删除一个字符串的所有匹配项。如果我发现某个事件,则要求我传递1;如果我没有发现,则要求传递0。我尝试使用两个循环来迭代输入字符串和需要删除的子字符串。如果两个字符串在某个索引上不相等,我们将继续迭代。如果它们相等,则尝试“编辑”结果字符串,但没有成功。 我将感谢任何形式的帮助! 这是我的代码:已编辑

#include <stdio.h>
#define STRING_SIZE 100

int str_remover(char result_string[STRING_SIZE], char pattern_str[STRING_SIZE]) {
    int i = 0, j = 0, k, t = 0, q = 0, counter = 0, f = 0;
    int is_found = 0, found, toRemoveLen = 0, stringLen = 0;

    for (i = 0; result_string[i] != '\0'; i++) { // checking len of the input str
        stringLen += i;
    }

    i = 0;

    for (i = 0; pattern_str[i] != '\0'; i++) { // checking len of the pattern str
        toRemoveLen += i;
    }

    i = 0;

    for (i = 0; i <= stringLen - toRemoveLen; i++) // iterting over the input str
    {
        found = 1;
        for (j = 0; j < toRemoveLen; j++) // iterating over the pattern str
        {
            if (result_string[i + j] != pattern_str[0]) // if they're diffrent, found should be 0 and we break the loop
            {
                found = 0;
                break;
            }
            else { // if the first letter is equal, we can check the rest
                f = i + j; // new int 
                while (result_string[f] == pattern_str[q]) { // we'll count while they are equal
                    f++;
                    q++;
                    counter++;
                }
                if (counter == toRemoveLen) { // if they were equal the same amount as the len of the pattern string, we found an occorence
                    is_found = 1; // if we found even one occurence, we'd like to return 1 for the func in main
                    for (k = i; k < toRemoveLen; k++)
                    {
                        result_string[k] = result_string[k + toRemoveLen];
                    }
                }
            }
        }
    }
    result_string[i] = '\0';
    return is_found;
}

int main() {
    char result_string[STRING_SIZE] = { 0 }, char1[STRING_SIZE] = { 0 }, char2[STRING_SIZE] = { 0 };
    printf("Please enter the main string..\n");
    gets(char1);
    printf("Please enter the pattern string to find..\n");
    gets(char2);
    int is_stripped = 0;
    is_stripped = str_remover(char1, char2, result_string);
    printf("> ");
    printf(is_stripped ? result_string : "Cannot find the pattern in the string!");
    return 0;
}
#包括
#定义字符串大小为100
int str_remover(字符结果字符串[字符串大小]、字符模式字符串[字符串大小]){
int i=0,j=0,k,t=0,q=0,计数器=0,f=0;
int是_found=0,found,toRemoveLen=0,stringLen=0;
对于(i=0;result_string[i]!='\0';i++){//检查输入str的len
stringLen+=i;
}
i=0;
对于(i=0;pattern_str[i]!='\0';i++){//检查模式str的len
toRemoveLen+=i;
}
i=0;
对于(i=0;i检查

您可以这样做:

     #include <stdio.h>
#include <string.h>
void main(){
    int i, j = 0, k = 0;
    char str[100], key[20];
    char str1[10][20];
    printf("Please enter the main string..\n");
    scanf("%[^\n]s",str);
    for (i = 0; str[i]!= '\0'; i++){
        if (str[i]==' '){
            str1[k][j] = '\0';
            k++;
            j = 0;
        }else{
            str1[k][j] = str[i];
            j++;
        }
    }
    str1[k][j] = '\0';
    printf("Please enter the pattern string to find..");
    scanf("%s", key);
    for (i = 0;i < k + 1; i++){
        if (strcmp(str1[i], key) == 0 || strstr(str1[i],key) != NULL){
            for (j = i; j < k + 1; j++)
                strcpy(str1[j], str1[j + 1]);
        k--;
        }
    }
    for (i = 0;i < k + 1; i++){
        printf("%s ", str1[i]);
    }
    printf("\n");
}
#包括
#包括
void main(){
int i,j=0,k=0;
字符str[100],键[20];
char-str1[10][20];
printf(“请输入主字符串..\n”);
scanf(“%[^\n]s”,str);
对于(i=0;str[i]!='\0';i++){
如果(str[i]=''){
str1[k][j]='\0';
k++;
j=0;
}否则{
str1[k][j]=str[i];
j++;
}
}
str1[k][j]='\0';
printf(“请输入要查找的模式字符串”);
scanf(“%s”,键);
对于(i=0;i
  • 将输入字符串拆分为单词
  • 将提取的单词与关键字进行比较
  • 添加了strstr()以在单词中提取

  • 您的str的“len”是
    (字符串长度)*(字符串长度-1)/2
    ,右侧注意:您不应该使用
    gets()
    ,它有不可避免的缓冲区溢出风险,在C99上被弃用并从C11中删除。
    for(i=0;input_str[i]!='\0';i++{//检查输入str stringLen i+=i;}
    这没有多大意义,你要添加递增的
    i
    你应该添加1,或者简单地使用
    i
    @anastaciu…的最终值,或者简单地使用
    strlen()
    @MikeCAT,啊,好的,这肯定是那些“赋值”之一。谢谢!!这很好,但是如果我想从中删除“str”“一个字符串就是一个字符串”我仍然会得到“一个字符串就是一个字符串”而不是“一个字符串就是一个字符串”…@raz查看我编辑的代码。它也会在每个单词中删除。好的,实际上它不起作用):而且我发现除了stdio之外我不能使用任何库。我在我的帖子中编辑了我的代码