c - 尝试从 C 中的字符串中删除子字符串,但始终失败

标签 c arrays string substring

我知道这个问题之前已经被问过很多次了,但我就是无法理解我做错了什么。每当我取得一些进展时,我就会收到一个新错误。我使用的代码非常基本,因为我是新手,我们的教授要求使用 scanf 和 gets。这是我到目前为止的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
int identify(char[], char[]);
int remove(char[], char[], int);
int scan(choice)
{
    while(choice < 0 || choice > 7)
    {
        printf("Invalid input, choose again\n");    
        scanf("%d", &choice);
    }
    return choice;  
}


int main()
{
    char sentence[MAX_SIZE], word[MAX_SIZE];
    int choice, i, j, k, deikths;

    printf("Choose one of the following:\n");
    printf("1. Give sentence\n");
    printf("2. Subtract a word\n");
    printf("3. Add a word\n");
    printf("4. Count the words\n");
    printf("5. Count the sentences\n");
    printf("6. Count the characters\n");
    printf("7. Is the phrase a palindrome?\n");
    printf("0. Exit\n");
    scanf("%d", &choice);
    if(scan(choice) == 1)
    {
        printf("Give sentence:\n");
        gets(sentence);
        gets(sentence);
        printf("%s\n", sentence);
    }
    else(scan(choice) == 2);
    {
        printf("Give word you want to subtract\n");
        gets(word); 
        printf("%s", word);
        deikths = identify(sentence, word);
        if(deikths != -1)
        {
            remove(sentence, word, deikths);
            printf("Sentence without word: %s\n", sentence);
        } 
        else
        {
            printf("Word not found in sentence.\n");
        }
    }
}

int identify(char sentence[], char word[])
{
    int i, j, k;
    for(k = 0; word[k] != '\0'; k++);
    {
        for(i = 0, j = 0; sentence[i] != '\0'; i++)
        {
            if(sentence[i] == word[j])
            {
                j++;
            }
            else
            {
                j = 0;
            }
        }
    }
    if(j == 1)
    {
        return(i - j);
    }
    else
    {
        return -1;
    }
}

int remove(char sentence[], char word[], int deikths)
{
    int i, k;
    for(k = 0; word[k] != '\0'; k++)
    {
        for(i = deikths; sentence[i] != '\0'; i++)
        {
            sentence[i] = sentence[i + k + 1];
        }       
    }
}

我收到的错误是删除函数具有冲突的类型。任何有关修复我的代码的帮助将不胜感激,甚至对我的问题的替代解决方案也会很棒。

最佳答案

正如注释中所确定的,由于 remove 已在 stdio.h 中定义,因此会生成编译器错误。更改后,代码编译成功,但仍然无法按预期工作。

identify 是一个函数,用于查找字符串中是否存在子字符串并返回其位置。这与标准库中的 strstr 的工作方式非常相似 - 我建议查看该函数的实现,以更好地理解这是如何完成的。 您实现的函数只能正确查找字符串末尾长度为 1 的子字符串。我在下面的代码中突出显示了导致此问题的错误。

int identify(char sentence[], char word[])
{
    int i, j, k;
    for(k = 0; word[k] != '\0'; k++); // <- this loops is never actually ran because of the trailing semicolon - this is however a good thing as it is redundant
    {
        for(i = 0, j = 0; sentence[i] != '\0'; i++)
        {
            if(sentence[i] == word[j])
            {
                j++;
            }
            else
            {
                j = 0; // <- this makes it so only matches at the end can be found - otherwise, j is just reset back to 0
            }
        }
    }
    if(j == 1) // <- this makes it so only matches of length 1 can be found
    {
        return(i - j); // <- this is only correct if the match is at the end of the sentence
    }
    else
    {
        return -1;
    }
}

strremove 由于嵌套循环而效率低下,并且需要缩短复制的字符范围 - 现在数据访问超出了数组末尾。

int strremove(char sentence[], char word[], int deikths)
{
    int i, k;
    for(k = 0; word[k] != '\0'; k++) // <- this loop is redundant
    {
        for(i = deikths; sentence[i] != '\0'; i++) // <- you need to add range checking to make sure sentence[i+k+1] doesn't go beyond the end of the string
        {
            sentence[i] = sentence[i + k + 1];
        }       
    }
}

我会将 main 中的问题作为练习留给您 - 毕竟这是一项作业。

关于c - 尝试从 C 中的字符串中删除子字符串,但始终失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53381873/

相关文章:

c - 调试警告 : String is not null terminated. 知道它是什么吗?

c - 遍历字符串与打印特定位置时的不同结果 (C)

java - 从字符串更改为对象名称 Java

string - 在 clojure 中应用 str 时保留空格

javascript - 从 javascript 返回一个数组

linux - bash 打印和递增数组值

c - 未决请求队列存储在哪里?

c - 有效地交换半字

php - PHP字符串索引数组如何在C++中高效实现

Javascript减少一个空数组