-1

I'm writing a code that will take a word and a text to be examined from stdin and censored the word if it appears in the text. That's my code so far, but when I compile the code it produces error below.

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

int main(int argc, char* argv[])
{
    char censor[] = "CENSORED";
    char input[1028];

    for(int i = 1; i < argc; i++){
        if(strstr(input, argv[i]) != NULL){
            for(int j = 0; j < strlen(input); j++){
                if(strcmp(input[j], argv[i]) == 0){
                    input[j] = censor;
                }
            }
        }
    }

    printf("%s", input);
    printf("\n");
}

censored.c: In function ‘main’: censored.c:13:15: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion] if(strcmp(input[j], argv[i]) == 0){ ^ In file included from censored.c:3:0: /usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘char’ extern int strcmp (const char *__s1, const char *__s2) ^ censored.c:14:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion] input[j] = censor;

I'm not sure why they think the char array is an integer, please help, thanks!

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
Elizabeth
  • 1
  • 1
  • 2
    `input[j]` means to select the j-th character out of the array. So you are passing a single character, not an array. I guess you meant `input + j` – M.M Aug 09 '17 at 03:57
  • `strstr(input, argv[i]) != NULL` use of uninitialized string? – Ajay Brahmakshatriya Aug 09 '17 at 04:09
  • arguments of `strcmp` are `const char *str1, const char *str2`, you are using only a char in `input[j]` – Tisp Aug 09 '17 at 04:13
  • possible duplicate of https://stackoverflow.com/questions/27160073/replacing-words-inside-string-in-c – Sahib Yar Aug 09 '17 at 05:11
  • you might be thinking that you can use `input[j]` the way that you can / do use `argv[i]` (in that same function call); but note that `argv[]` is a `char*` and `input[]` is a `char` – landru27 Jun 20 '19 at 15:47

2 Answers2

1

As error suggests, there are two problems in your code.

  1. In line 13: input[j] is a character and we should pass char * like &input[j] or (input+j) or input.

  2. In line 14: You can not directly copy one string into other by "=".

You may refer below code for your functionality.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
    char censor[] = "CENSORED";
    char word[] = "bad_word";
    char input[1028];
    /* added logic code */
    for(int i = 1; i < argc; i++) {
            /* compare if this is bad word */
            if(strcmp(argv[i], word) == 0) {
                    // found bad_word
                    //replace with censor
                    strcat(input, censor);
                    strcat(input, " ");
            } else {
                    // no bad word, we can go with the same
                    strcat(input, argv[i]);
                    strcat(input, " ");
            }
    }
    printf("%s", input);
    printf("\n");
}

So run your code from terminal as

./a.out I found bad_word and this is bad_word

And it will give output as

I found CENSORED and this is CENSORED
DevNP
  • 21
  • 4
0

as note in your error mention you are passing char instead of const char*, note: expected ‘const char *’ but argument is of type ‘char’ extern int strcmp

here char input[1028]; is array of character . so you need to pass address of array index i.e &input[j] instead of input[j].

strcmp(&input[j], argv[i]) should work in place of

strcmp(input[j], argv[i])

note : here argv[i] is array of char* so you don't need to pass it as &argv[i].Also in array case if you are mentioning array index then you need to add & to pass its address.if you are passing array without mentioning index i.e strcmp(input,argv[i]) in your case then it will pass address of input[0]