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

时间:2020-05-05 18:02:07

标签: c

我正在尝试编写代码,以从另一个字符串中删除每次出现的字符串。如果我发现一个事件,则要求我传递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;
}

1 个答案:

答案 0 :(得分:0)

选中this

您可以改为这样做:

     #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");
}
  1. 将输入字符串分割成单词
  2. 将提取的单词与关键字进行比较
  3. 添加了strstr()以提取单词