How can I find words from scrambled letters?Visual Basic 2010 - arrays

I have scrambled the letters, so they're different each time and have a txt file which contains all of the words from the dictionary. I am new to this and trying to teach myself VB, but this has got me, so could really do with some help.
Basically, how can I make sure that what the player enters into the textbox is a word that can be found in the scrambled 8 letters by comparing against the string and also the txt file to check that it is a real word? The words can be any length, as long as they're less than 8.
I have two arrays, one for consonants and one for vowels, so the player creates the scrambled letters by clicking on either the vowel button or consonant button.
An example being:
KEEIAQWL
The word LEAK is present.
:)

One way would be to check to individual letters as they are entered into the textbox by the user; if they don't exist don't add them - that way you will never need to check before your dictionary lookup.
A general way would involve looking at each character in the submitted word;
available_letters = "KEEIAQWL"
....
entered_word = "leak"
available_letters_temp = available_letters
entered_word = Ucase$(entered_word) '//ensure same case
dim i as long, pos as long
for i = 1 to Len(entered_word)
'// see if letter ok
pos = instr(1, available_letters_temp, mid$(entered_word, i, 1))
if pos = 0 then
msgboxMid$(entered_word, i, 1) & " is not valid"
else
'// ok, remove for future lookups
mid$(available_letters_temp, pos, 1) = "#"
end if
Next
This also makes sure that a letter can only be used once, i.e. "BOOB" from "BO" is not allowed.

Related

Iterate through string

I am having difficulties in Ruby for an assignment(we just started to see it). I think I am close to the answer but something is not right with how I stor emy string elements.
So the assignment is to have a method HID_Num(String) that takes a string.
To be a valid string it needs to have: 4 uppercase letters, followed by 8 digits number (e.g.: "ABCD18347692").The method should return the individual number as an integer value, or nil if no individual valid card number is found.
Ex: HID_num ("ADBC12345678 abcD12345678") should return 12345678
my code so far:
def HID_num(str)
str.gsub(/\s+/m, ' ').strip.split(" ")
str.each { |element|
if(element.count == 12)
count = str.count("A-Z")
if(count == 4)
count2 = str.count("1-9")
if(count == 8)
puts str[3...11]
else{
puts "nil"
} }
end end
What'S wrong with it: does not store the element of str.gsub.
I was thinking to used pointers but not sure how to used them OR to store each element in an array but I have no idea how... Maybe I am thinking too much like Java coding?
Thanks!
This is pretty easy to do with regular expressions. Here's an example:
def hid_num(str)
exp = /\A[a-z]{4}(\d{8})\z/i
match = str.match(exp)
match && match[1]&.to_i
end
In this case, the expression exp matches strings beginning with 4 alphabet characters, followed by 8 digits, to the end of the string. Next, we grab a match on the exp against the provided str. If there is not a match, return nil; otherwise, return the digit portion of the string, cast to an integer value.
Hope that helps!
You could use:
def HID_num(string)
entries = string.split(" ")
entries.map do |entry|
matches = entry.match(/\A[A-Z]{4}(\d{8})\z/)
next unless matches
matches[1]
end.compact
end
It's a fairly simple regex matcher - this will return an array of all the valid strings' numbers. If you want to tweak it to return just the first valid number, you can change map for each and add break ahead of the line matches[1], and finally remove the compact.
Hope that helps!

How to split sentences in an array

I have a string s which stores a very long sentence and I want to copy the content of s to an array C with each cell storing a sentence each. The following is my code which is not giving me any output, but the dimension of the cell:
while(i<6)
C(i)=s;
end
This is how I get as output when I print C:
C=
[1x76 char]
Can somebody please help me.
Another job for strsplit:
>> sentences = 'This is the first one. Then here is a second. Yet another here.';
>> C = strsplit(sentences,'. ')
C =
'This is the first one' 'Then here is a second' 'Yet another here.'
We are specifying a period followed by a space as the delimiter. Change this as needed.
Suppose Long string is:
longString = "This is first cell. This is second cell. this is third cell".
Now since . is delimiter here means it is acting as separator for sentences. so you can loop through longString character wise and whenever you encounter a . you just increase Array index count and keep storing in this Array index until you find another .
here is sudo code:
array[];
index = 0;
loop through(longString) character wise
{
if(currentChar equals to '.')
{
index++;
}
else
{
array[index] = currentChanracter;
}
}

matlab divide sentences into words

Im new in matlab and ım trying to take the input from matlab gui which will be entered by a user and divide that sentence into words but I need to have them as letters because Im using a robot to write them. this letters will be send to these robots. Im using two robots and for example when I write 'lou reed' in text when ı press the button matlab function will hold this 2 words in to different char arrays so that ı can have the letters c(i) like this and send them to process. so far ı wrote these but ım stuck.
c = char(get(handles.edit1,'String'));
int count1;
int count2;
char word1;
char space=" ";
for i=1:length(c)
int t = isequal(c(i),space);
if(t==0)
count1=count1+1;
word1=;%ım trying to add the char here to find the new word
else
end
end
ı dont know what to do ı searched but ı couldnt find something usefull maybe ı wasnt looking right.
Anything would be helpful, thankss
What characters are allowed? First you should remove all the characters that are not allowed (substitute them with a space character?). After that just this:
str = ' Once upon a time ';
words_in_str = textscan(str,'%s');
words_in_str{1}
If you have a newer version of MATLAB (greater than 2012a I think), you can use strsplit
characterString = 'lou reed';
C = strsplit(characterString);
C will be a cell array with each element being a separate word.
You can simply find the space characters in your string with
mystring = 'Hello Cruel World';
spaces = find(mystring==' ');
The variable spaces is now a vector pointing to where each of your word breaks are. If you want to break this up into words, you could use
mystring = 'Hello Cruel World';
wordboundaries = [0,find(mystring==' ')];
wordlen = diff([wordboundaries,length(mystring)+1])-1;
numwords = length(wordboundaries);
for w = 1:numwords
idx = wordboundaries(w) + (1:wordlen(w));
word{w} = mystring(idx);
end
display(word);
Now word is a cell array containing the individual words.

Find longest suffix of string in given array

Given a string and array of strings find the longest suffix of string in array.
for example
string = google.com.tr
array = tr, nic.tr, gov.nic.tr, org.tr, com.tr
returns com.tr
I have tried to use binary search with specific comparator, but failed.
C-code would be welcome.
Edit:
I should have said that im looking for a solution where i can do as much work as i can in preparation step (when i only have a array of suffixes, and i can sort it in every way possible, build any data-structure around it etc..), and than for given string find its suffix in this array as fast as possible. Also i know that i can build a trie out of this array, and probably this will give me best performance possible, BUT im very lazy and keeping a trie in raw C in huge peace of tangled enterprise code is no fun at all. So some binsearch-like approach will be very welcome.
Assuming constant time addressing of characters within strings this problem is isomorphic to finding the largest prefix.
Let i = 0.
Let S = null
Let c = prefix[i]
Remove strings a from A if a[i] != c and if A. Replace S with a if a.Length == i + 1.
Increment i.
Go to step 3.
Is that what you're looking for?
Example:
prefix = rt.moc.elgoog
array = rt.moc, rt.org, rt.cin.vof, rt.cin, rt
Pass 0: prefix[0] is 'r' and array[j][0] == 'r' for all j so nothing is removed from the array. i + 1 -> 0 + 1 -> 1 is our target length, but none of the strings have a length of 1, so S remains null.
Pass 1: prefix[1] is 't' and array[j][1] == 'r' for all j so nothing is removed from the array. However there is a string that has length 2, so S becomes rt.
Pass 2: prefix[2] is '.' and array[j][2] == '.' for the remaining strings so nothing changes.
Pass 3: prefix[3] is 'm' and array[j][3] != 'm' for rt.org, rt.cin.vof, and rt.cin so those strings are removed.
etc.
Another naïve, pseudo-answer.
Set boolean "found" to false. While "found" is false, iterate over the array comparing the source string to the strings in the array. If there's a match, set "found" to true and break. If there's no match, use something like strchr() to get to the segment of the string following the first period. Iterate over the array again. Continue until there's a match, or until the last segment of the source string has been compared to all the strings in the array and failed to match.
Not very efficient....
Naive, pseudo-answer:
Sort array of suffixes by length (yes, there may be strings of same length, which is a problem with the question you are asking I think)
Iterate over array and see if suffix is in given string
If it is, exit the loop because you are done! If not, continue.
Alternatively, you could skip the sorting and just iterate, assigning the biggestString if the currentString is bigger than the biggestString that has matched.
Edit 0:
Maybe you could improve this by looking at your array before hand and considering "minimal" elements that need to be checked.
For instance, if .com appears in 20 members you could just check .com against the given string to potentially eliminate 20 candidates.
Edit 1:
On second thought, in order to compare elements in the array you will need to use a string comparison. My feeling is that any gain you get out of an attempt at optimizing the list of strings for comparison might be negated by the expense of comparing them before doing so, if that makes sense. Would appreciate if a CS type could correct me here...
If your array of strings is something along the following:
char string[STRINGS][MAX_STRING_LENGTH];
string[0]="google.com.tr";
string[1]="nic.tr";
etc, then you can simply do this:
int x, max = 0;
for (x = 0; x < STRINGS; x++) {
if (strlen(string[x]) > max) {
max = strlen(string[x]);
}
}
x = 0;
while(true) {
if (string[max][x] == ".") {
GOTO out;
}
x++;
}
out:
char output[MAX_STRING_LENGTH];
int y = 0;
while (string[max][x] != NULL) {
output[y++] = string[++x];
}
(The above code may not actually work (errors, etc.), but you should get the general idea.
Why don't you use suffix arrays ? It works when you have large number of suffixes.
Complexity, O(n(logn)^2), there are O(nlogn) versions too.
Implementation in c here. You can also try googling suffix arrays.

C programming: function that keeps track of word lengths & counts?

I'm having trouble articulating what the function is supposed to do, so I think I will just show you guys an example. Say my program opens and scans a text file, which contains the following:
"The cat chased after the rooster to no avail."
Basically the function I'm trying to write is supposed to print out how many 1 letter words there are (if any), how many 2 letter words there are, how many 3 letter words, etc.
"
Length Count
2 2
3 3
5 2
6 1
7 1
"
Here's my attempt:
int word_length(FILE *fp, char file[80], int count)//count is how many total words there are; I already found this in main()
{
printf("Length\n");
int i = 0, j = 0;
while(j < count)
{
for(i = 0; i < count; i++)
{
if(strlen(file[i] = i)
printf("%d\n", i);
}//I intended for the for loop to print the lengths
++i;
printf("Count\n");
while()//How do you print the counts in this case?
}
}
I think the way I set up the loops causes words of the same length to be printed twice...so it'd look something like this, which is wrong. So how should I set up the loops?
"Length Count
2 1
2 2
"
This sounds like homework, so I will not write code for you, but will give you some clues.
To hold several values you will need array. Element with index i will contain counter for words with length i.
Find a way to identify boundaries of words (space, period, beginning of line etc.). Then count number of characters between boundaries.
Increase relevant counter (see tip 1). Repeat.
Some details. You actually want to map one thing to another: length of word to number of such words. For mapping there is special data type, called usually hash(table) or dictionary. But in your case array can perfectly work as a map because you keys are uniform and continues (1,2 ... to some maximum word length).
You can't use a single int to count all of that. You need an array and then in it at position 0 you keep track of how many 1 letter words, at position 1 you accumulate 2 letter words and so on.

Resources