in my C application, I have a string which I want to compare to a list of strings in my application. the list of strings can be hardcoded in my C application.
what is the best way to define the list of strings in my application?
what is the best way to compare the string in my application to the list of strings?
I you are after efficiency, you should use a trie, it will give you O(|S|) search time to match your input string to the given set of strings. [where |S| is the length of the input string]
If you are after quick coding, just store the strings in a predefined char*[], and iterate over it with strcmp()
What do you mean by "compare a string to a list of strings"? The two are different kinds of "objects", they are not obviously comparable.
How does the string "372" compare to the list of strings { "12, "42", "-11", "372" }?
If you mean "look for a string in a list of strings", then the best bet would be to sort the list, then use binary-search to quickly check if the candidate is present.
A list of strings is easily represented in C as an array of character pointers, char *strings[]. You can use qsort() to sort, and bsearch() to search.
If you want excessive speed, and you're willing to use extra tools, you could consider gperf. Given a list of strings, it works out a very fast inclusion test based on a hash function, and spits out the C code for it.
Related
I have a string "abcdbca" and I'm instructed to slice two subarrays, say [0:3] and [4:7], I get strings "abc" and "bca". I've to find out if the two substrings are similar(same elements, max_allowed_mismatch_error = 1).
I tried count sort, but it's not that much of optimization. So, I though the next more optimized method could be hashing. But I can't figure out hash function to accurately solve the problem. I need to perform the operation several times.
Hashing is no good.
There are two solutions, the simple one, which is to insist that the sub strings be of equal length and count equal characters, and the complex one, which is to use an alignment algorithm like Needleman-Wunch. That will give a much more robust idea of string similarity.
I have an ArrayList that is populated with strings from a text file, and now I want to sort it alphabetically (I can't use collections.sort or anything like that, it has to be manual).
What is the correct way to compare two elements of an ArrayList (if there is one at all)?
I'm sure there may be a matlab function to do this but I'm required to write my own. As the title says, I need to write a function which when given a single cell array of strings, returns a structure array, containing the same strings but in alphabetical order. Furthermore, the 'count' fields must contain the number of times that that particular string has occurred eg
z=myfunction({'bag','dig','bag'})
ans =
str: 'bag'
count = 2
Ideally, the method should have an expected number of comparisons for n strings of O(n log n)
Assuming you don't want to use standard functions like sort or unique this is not an easy question. Furthermore it is more about math then about programming.
If you just want to practice programming, try implementing something simple like bubble sort.
This will not achieve O(n log n) however, if you really want that look into merge sort for example.
Several options are explained roughly here, and with a bit of searching it should not be hard to find what you need.
This is a small section of a question that I was asked once. I have a variable array of strings like
str list []={abcd,xyzw,qwer,abcde}
And my input is:
input list[]={ab,abc,q,z,x}
Output should be[]={abcd,abcd,qwer,-,xyzw}
Each input string should be matched to the same characters (from beginning) in the list. It should give the first available string as the answer.
The working approached I could think of were:-
Brute force: Time complexity O((number of strings in list)*(average length of input strings)*(number of input strings))
Hashing: It too is taking the same time.
Is there a better way to do this?
if your list[] is fix (or does not change very much), a trie would do the trick. While inserting, you have to do a bit of logic to identify the "first match", so if "abcd" is the first, you should not insert "abcde" (or invalidate it, if "abcd" could be discarded, so you have to do a bit of accounting and bookkeeping).
details: http://en.wikipedia.org/wiki/Trie
I have to organize a struct or an array of some people's first name, last name, and age then organize them in alphabetical order writing them from an input file to an output file using the string library.
This is partically for a Lab that I have tomorrow afternoon in which my TA said we might be asked to complete the task I stated above. I'm trying to get opinions or suggestions from individuals whom are much more experienced in C than myself so I'll be more prepared than I was last lab where I didn't so hot.
I'm stuck, any suggestions on where I should start?
Well, reading and writing the input and output should be a no brainer (you open the file, read/write and then close it when you're done with it).
The trick is to get those pesky strings sorted.
The way I look at strnig sorting is an array of arrays sorting. The first array-level is an iteration over all the structs while the second is an iteration over all the letters of a name (note that some names are longer then others)
1. First you sort the first column - all the first letters
2. Then all the sub-columns - within each first letter group you sort the second letter
3. Repeat 1&2 until you run out of letters to sort.
The sorting of an array of letters is the same as sorting an array of byte (AKA chars), and if you're working with an algorithm like bubble sort, working on a sub array is seamless.
Hope this idea helps
You can use strcmp from string.h library, which compares two strings and returns the result.
You should read every string from input file and keep in memory. You will probably use a list for that, for example linked list. While adding you can organize them in alphabetical order. Here is an example:
Take the an element in list.
Compare it with new string.
Where should it be placed? if it should be before the current element, place it before the current element. If it should be after current element, do the same for next element.
This is a simple algorithm for your problem.
The tricky part is to implement the comparation of two strings. the strcmp function only tells you whether the strings are identical. You can loop through the char array to do it.
Once this is done, the remaining sorting is simple.