-2

How can I find a word in a char array and censor it with wildcard (*) characters?

I've tried to find the first occurrence of the word, but failed. I've tried this and it didn't work either. I'm a newbie and I've been trying this for 5 hours.

int main()  
{
    int w,q;
    char l,m;
    char *arr, *swear;
    int i,a,t,k,z;

    printf("Enter a word which is not allowed.");
    scanf("%s", swear);
    printf("Now enter a word.");
    scanf("%s", arr);

    for(a=0; swear[a]!='\0'; ++a); //finding length of swear
    for(t=0;arr[t]!='\0';t++);    // finding length of arr

    for(i=0,k=0;k<t & i<t;i++,k++)
    {
        arr[i]=l;
        swear[k]=m;
        if(strstr(swear,arr))
            arr[i]='*';
        else
            break;
    }

    for(z=0;z<t;z++)
    {
        printf("%c",arr[z]);
    }
    return(0);
}
jab
  • 396
  • 3
  • 15
Anil Koc
  • 11
  • 4
  • 1
    This is not how comments work. Comments are there to explain why you are doing what you're doing and not what you're doing. Might aswell get used to explain why and not what. – Kent Kostelac Mar 08 '16 at 10:33
  • Please: show more code, especially how is `arr` declared. Read about [MCVE](http://stackoverflow.com/help/mcve). – Jabberwocky Mar 08 '16 at 11:11
  • Thanks I edited my answer – Anil Koc Mar 08 '16 at 11:17
  • `scanf` needs a big enough buffer to store strings not a dangling pointer. Try something like `char arr[1024], swear[1024];` and to prevent `scanf` writing out of the buffer `scanf("%1024s"`, swear);` – Manos Nikolaidis Mar 08 '16 at 11:54

1 Answers1

3

So you want to overwrite all occurrences of a string with an equal length string composed of '*'. For that you need to iteratively get pointers to occurrences of the string and you also need its length to know how many '*' you have to use.

size_t swearLength = strlen(swear); // swear is assumed null-terminated
char *here = arr;
while ((here = strstr(here, swear)) {
    memset(here, '*', swearLength);
    here += swearLength;  // to avoid searching what's already censored
}

strstr will return null if it can't find swear so the loop will terminate

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • 1
    suggestion, after memset line, add: `here += swearLength;` No point rechecking that which you have just written. – weston Mar 08 '16 at 10:43
  • Looks good to me, downvoter care to enlighten? – weston Mar 08 '16 at 10:43
  • Same problem as the other answer: `strstr` is not a *word* function. – Jongware Mar 08 '16 at 11:22
  • @RadLexus To be far, the OP's code doesn't do that, nor does he ask for it. – Alnitak Mar 08 '16 at 11:48
  • @Alnitak: "How can I find a *word* in a char array ..." That said, that task alone is worth a short doctoral dissertation – and has been asked many, many times before on SO. Looking at OP's code I concur it's by far not the main problem (even thought OP spent a whopping whole "5 hours" on it; I have *thought* about specific problems far longer than that). – Jongware Mar 08 '16 at 12:54