# Word search program in C



## DizitalNovice (Mar 26, 2008)

I want to make a program in C which will take a string input and replace predefined words(available in a text file) with *s. Can be used to replace offensive words in a file,etc. But I don't get the word searching algo. ie After I read a word from the input, how do I search whether that word is in the given list or not? Please help!


----------



## mehulved (Mar 26, 2008)

DizitalNovice said:


> I want to make a program in C which will take a string input and replace predefined words(available in a text file) with *s. Can be used to replace offensive words in a file,etc. But I don't get the word searching algo. ie After I read a word from the input, how do I search whether that word is in the given list or not? Please help!


See this source code - ftp://tron.um.u-tokyo.ac.jp/pub/GNU/grep/ or *www.opensource.apple.com/darwinsource/10.4.6.x86/grep-14/grep/src/


----------



## DizitalNovice (Mar 26, 2008)

I cud not understand a thing in the links. Can someone offer something simpler.I'm looking for a simple short program. Can't write 10 files for this stuff!


----------



## QwertyManiac (Mar 26, 2008)

An algorithm for searching the file you mention having the list line by line:


```
While file-pointer != EOF
    While readchar() != '\n' // New line char
        Keep reading char
    //Now if \n is encountered
    Check if word input == readchar() array so far and perform necessary action
    // Clear readchar() array
Loop
```


----------



## DizitalNovice (Mar 27, 2008)

@qwertymaniac
Thanks for your help! I still have a doubt. Your program will check for a word input by the user to match words in a file , but what I want is that the list of words to be checked exists in a file (say offensive_words.txt) and the user inputs a string. Then we check the words in the user's string one by one to check whether any word in the offensive_words.txt file is present there.If it is then we replace the whole word with *s. and then print the modified String.


----------



## QwertyManiac (Mar 27, 2008)

Um why don't you look at string.h's functions for a change? Its got all those find(), strcmp() functions in it which would be exactly what you require.

And as for breaking the input into words, make a temp variable store characters only until a space is encountered and do the operation on that and then in the next iteration clear that temp and loop.

You can also store the file contents temporarily post reading into a Linked-List or a string array.


----------



## Garbage (Mar 27, 2008)

why not to use strstr() to check for substring ?


----------

