简体   繁体   English

尝试从C中的字符串中删除子字符串,但仍失败

[英]Trying to remove substring from string in C, keep failing

I know this question has been asked many times before but I simply cannot get my head around what I am doing wrong. 我知道这个问题已经被问过很多次了,但是我根本无法理解我做错了什么。 Everytime I make some progress I get a new error. 每当我取得一些进展时,我都会收到一个新错误。 The code I am using is really basic because I am a newbie and our professor requires the usage of scanf and gets. 我使用的代码非常基础,因为我是新手,我们的教授要求使用scanf并获取。 This is my code so far: 到目前为止,这是我的代码:

#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];
        }       
    }
}

The error I am getting, is that the remove function has conflicting types. 我得到的错误是remove函数的类型冲突。 Any help with fixing my code will be greatly appreciated, or even an alternative solution to my problem would bre great. 修复我的代码的任何帮助将不胜感激,甚至可以替代解决我的问题的方法。

As established in the comments, the compiler error is generated because remove is already defined in the stdio.h . 如注释中所确定的那样,由于在stdio.h已经定义了remove ,因此会生成编译器错误。 After changing, the name the code compiles successfully, but still doesn't work as expected. 更改后,代码名称可以成功编译,但仍无法按预期工作。

identify is the function which is meant to find whether a substring exists in a string and return its position. identify是用于查找字符串中是否存在子字符串并返回其位置的函数。 This is very similar to how strstr from the standard library works - I'd suggest having a look at an implementation of that function, to better understand how this is done. 这与标准库中strstr的工作原理非常相似-我建议您看一下该函数的实现,以更好地了解它是如何完成的。 The function you implemented only correctly finds substrings of length 1, at the end of the string. 您仅实现的函数只能在字符串末尾找到长度为1的子字符串。 I have highlighted errors in the code below which cause this. 我在下面的代码中突出显示了导致此问题的错误。

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 is inefficient due to the nested loops and the range of characters copied needs to be shortened - right now data is access beyond the end of the array. 由于嵌套循环, 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];
        }       
    }
}

I will leave the problems in main as an exercise to you - this is an assignment after all. 我会离开的问题, main是练习你-这毕竟是一项任务。

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

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