Reputation: 1
For example, I have an index text file that has 400+ English words, and then I have another text file with decrypted text on each line.
I want to check each English word in my index file with each line of my decrypted text file (so checking 400+ English words for a match per line of decrypted text)
I was thinking of using strncmp(decryptedString, indexString, 10)
because I know that strncmp terminates if the next character is NULL
.
Each line of my decrypted text file is 352 characters long, and there's ~40 million lines of text stored in there (each line comes from a different output).
This is to decrypt a playfair cipher; I know that my decryption algorithm works because my professor gave us an example to test our program against and it worked fine.
I've been working on this project for six days straight and this is the only part I've been stuck on. I simply can't get it to work. I've tried using
while(getline(&line, &len, decryptedFile) != -1){
while(getline(&line2, &len2, indexFile) != -1){
if(strncmp(decryptedString, indexString, 10) == 0){
fprintf(potentialKey, "%s", key);
}
}
}
But I never get any matches. I've tried storing each string in into arrays and testing them one character at a time and that didn't work for me either since it would list all the English words are on one line. I'm simply lost, so any help or pointers in the right direction would be much appreciated. Thank you in advance.
EDIT: Based on advice from Clifford in the comments, here's an example of what I'm trying to do
Let's say indexFile contains:
HELLO
WORLD
PROGRAMMING
ENGLISH
And the decryptedFile contains
HEVWIABAKABWHWHVWC
HELLOHEGWVAHSBAKAP
DHVSHSBAJANAVSJSBF
WORLDHEEHHESBVWJWU
PROGRAMMINGENGLISH
I'm trying to compare each word from indexFile to decryptedFile, one at a time. So all four words from indexFile will be compared to line 1, line2, line 3, line 4, and line 5 respectively.
Upvotes: 0
Views: 399
Reputation: 241701
If what you are trying to do is check to see if an input line starts with a word, you should use:
strncmp(line, word, strlen(word));
If you know that line
is longer than word
, you can use
memcmp(line, word, strlen(word));
If you are doing that repeatedly with the same word(s), you'd be better off saving the length of the word
in the same data structure as the word
itself, to avoid recomputing it each time.
This is a common use case for strncmp
. Note that your description of strncmp
is slightly inaccurate. It will stop when it hits a NUL in either argument, but it only returns equal if both arguments have a NUL in the same place or if the count is exhausted without encountering a difference.
strncmp
is safer than depending on the fact that line
is longer than word
, given that the speed difference between memcmp
and strncmp
is very small.
However, with that much data and that many words to check, you should try something which reduces the number of comparisons you need to do. You could put the words into a Trie, for example. Or, if that seems like too much work, you could at least categorize them by their first letter and only use the ones whose first letter matches the first letter of the line, if there are any.
If you are looking for an instance of the word(s) anywhere in the line, then you'll need a more sophisticated search strategy. There are lots of algorithms for this problem; Aho-Corasick is effective and simple, although there are faster ones.
Upvotes: 1
Reputation: 37232
If a line of decrypted text is 352 characters long and each word in the index is not 352 characters long, then a line of decrypted text will never match any word in the index.
From this I think you've misunderstood the requirements and asked a question based on the misunderstanding.
Specifically, I suspect that you want to compare each individual word in the decrypted line (and not the whole line) with each each word in your index, to determine if all words in the decrypted line are acceptable. To do that, the first step would be to break the decrypted line of characters into individual words - e.g. maybe finding the characters that separate words (spaces, tabs, commas?) within the decrypted text and replacing them with a zero terminator (so that you can use strcmp()
and don't need to worry about "foobar" incorrectly matching "foo" just because the first letters match).
Note that there's probably potential optimisations. E.g. if you know that a word from the decrypted text is 8 characters (which you would've had to have known to place the zero terminator in the right spot) and if your index is split into "one list for each word length" (e.g. a list of index words with 3 characters, a list of index words with 4 characters, etc) then you might be able to skip a lot of string comparisions (and only compare the word from the decrypted line with words that have the same length in the index). In this case (where you know both words have the same length already) you can also avoid modifying the original 352 characters (you won't need to insert the zero terminator after each word).
Upvotes: 0