0

i am really new to coding and was working on a supposedly simple problem of removing a character from a string in the c language. When I try to compile my code, I keep getting, error:conflicting types for 'remove'. I don't know why I am getting this error because the code seems okay. Help with this will be greatly appreciated. This is the code

#include <stdio.h>
#include <stdlib.h>

 int main()
{
    char ch,str[30],word[30];
    void remove(char[],char[],char);
    printf("enter the string\n");
    gets(str);
    printf("enter the character to move\n");
    ch=getchar();
    remove(str,word,ch);
    printf("converted to %s\n",word);

}

void remove(char str[], char word[],char c){
int j=0,k=0;
while(str[j++]!='\0'){
if(str[j]!=c)word[k++]=str[j];}
word[k]='\0';

}
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Welcome to SO! Don't call the function `remove` because it's already declared. Compiler says: `extern int remove (const char *__filename) __THROW;`. conflicts. See https://linux.die.net/man/3/remove. Also, don't use `gets` please. See [this](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – ggorlen Aug 25 '20 at 23:07

2 Answers2

1

The header <stdio.h> already has a declaration of a function named remove.

int remove(const char *filename);

So the compiler issues an error because the identifier remove is declared two times with different types in the same file scope.

So rename your function as for example remove_copy.

Nevertheless the function implementation is wrong.

Within the loop

while(str[j++]!='\0'){
if(str[j]!=c)word[k++]=str[j];}

you are comparing a next element str[j]!=c after current due to the increment in the condition

str[j++]

The function can be declared and implemented the following way

char * remove_copy( char s1[], const char s2[], char c )
{
    char *p = s1;

    for ( ; *s2; ++s2 )
    {
        if ( *s2 != c ) 
        {
            *p++ = *s2;
        }
    }
    
    *p = '\0';

    return s1;
}  

Pay attention to that the function gets is unsafe and is not supported by the C Standard any more. Instead use the standard function fgets.

Here is a demonstrative program.

#include <stdio.h>
#include <string.h>

char * remove_copy( char s1[], const char s2[], char c )
{
    char *p = s1;

    for ( ; *s2; ++s2 )
    {
        if ( *s2 != c ) 
        {
            *p++ = *s2;
        }
    }
    
    *p = '\0';

    return s1;
}  

int main(void) 
{
    enum { N = 30 };
    char str[N], word[N];
    char c;
    
    printf( "Enter a string: " );
    fgets( str, N, stdin );
    
    str[ strcspn( str, "\n" ) ] = '\0';
    
    printf( "Enter a character to remove from the string: " );
    c = getchar();
    
    printf( "The result string is \"%s\"\n", remove_copy( word, str, c ) );
    
    return 0;
}

Its output might look like

Enter a string: I am learning C++
Enter a character to remove from the string: +
The result string is "I am learning C"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1
  1. Change the function name. remove is reserved
  2. You function will not work anyway
#include <stdio.h>
char *strchrrem(const char *str, char *dest, char c)
{
    char *wrk = dest;
    do
    {
        if(*str != c) *wrk++ = *str;
    }while(*str++);

    return dest;
}

int main(void)
{
    char dest[64];

    printf("%s\n", strchrrem("Hello world.", dest, 'l'));
}

https://godbolt.org/z/exqdE4

0___________
  • 60,014
  • 4
  • 34
  • 74