So have an assignment that require me to read an input from a file and sort the input. So if I have a file containing:
banana
apple
orange
I have sucessfully insert those string to array. the array contain like this:
{'b','a','n','a','n','a','\n','a','p','p','l','e','\n','o','r','a','n','g','e'}
Now the part that confuse me, is that, I have to use qsort to sort those string. I have create a method that compare two array of char and it works. However, it successfully compare two different string. However, how can I use qsort to sort my array?
Related
I'm trying to write a function to change arrays to hash values, I've done it so that I can change a numerical array into their hash values but I can't seem to figure out how to change string arrays into their hash values.
I'm having trouble doing it with string arrays. for example if array= [aced]
this is my code so far.
function [h]=Hash31(array1)
n=length(array1);
i=1;
h=0;
if array1
for i=1:1:n
h=array1(i)+31*h;
end
end
and it turns a numerical array into their hash value.
I was wondering if I had to do some kind of conversion or logical to make it work
and if I did to that how would I fit it into the existing function.
would it have to be a logical, because I've tried using an if function but I don't know how to write in code 'if the array is string, then...' in a way that Matlab understands.
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)?
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.
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.
im trying to compare words of an array using strcmp.Im trying to get each word that appears more than once in the array to print out only once, so i can determine the number of unique words.I know what its doing wrong as when it searches the array it prints out each copy it finds, for example if the word "the" is in the array 4 times, it will print out 'the' 3 times and when string1 goes to the next location where 'the' is, it will print out 2 times and so on.
Convert your char arrays to std::string and instead of printing them, put them into an std::set. Then print each element in the set.
good that you added declarations,
now from what it looks it seems as if words[][] is redundant and makes things unnecessary complicated. if you are only interested in getting unique words, instead just process what comes back from strtrok by building up a dictionary with the encountered words
a dictionary could be something as simple as a max sized array containing unique words, and an index that starts at 0 when array is empty, whenever strtok returns a word, go through the array and look for the word using your strcmp, if it doesn't exist add it at the end of the array then increment then index.
and bob is your uncle :)