• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

Word search program in C

Status
Not open for further replies.

DizitalNovice

I don't want IT
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

18 Till I Die............
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 http://www.opensource.apple.com/darwinsource/10.4.6.x86/grep-14/grep/src/
 
OP
DizitalNovice

DizitalNovice

I don't want IT
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

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

Code:
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
 
OP
DizitalNovice

DizitalNovice

I don't want IT
@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

Commander in Chief
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.
 
Status
Not open for further replies.
Top Bottom