简体   繁体   中英

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. 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. 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 . 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. 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. The function you implemented only correctly finds substrings of length 1, at the end of the string. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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