• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# making letters out of a word [c#]

## Recommended Posts

Ok, since I could not use all the code I have been given and people have been telling me things that I already know and are not my question (probably due to my vague question) I am wondering how do I do this. string MainWord = "Computer"; string WordMadeOutOfMainWord=""; TextBox.Text = "out"; [button clicked] WordMadeOutOfMainWord=TextBox.Text.ToString(); IF THE WORD OUT CAN BE MADE OUT OF "COMPUTER" THEN TELL ME OTHERWISE; SAY U CANNOT MAKE THAT WORD. How do I do that in C#? Any help would be great! Thanks, Rob [edited by - rob64464 on March 24, 2004 12:34:47 PM]

##### Share on other sites
didn't you ask this in another thread yesterday? and this sounds suspiciously like homework for a comp sci class. so i'll give possible algorithms only till you explain how it's not homework

the basic algorithm is to take the words that the user enters and first make sure that they are actual words (you'll have to check a dictionary that you'll somehow have to load into memory. windows probably has some kind of plug-in dicitonary you can use). after verifying that it's a word (or just not checking in the case that you just want to get to the actual logic) you'll want to make sure that all of the letters of each word are found in the original word. a simple first test would be to make sure that each new word is not longer than the original word. so then check each letter to make sure it's in the original word. then do a letter count of each instance of the letters in the user's words and compare it to the letter count for that letter in the original word. if there are more a's than in the original, for instance, then mark it as incorrect.

-me

[edited by - Palidine on March 23, 2004 3:22:16 PM]

[edited by - Palidine on March 23, 2004 3:23:02 PM]

##### Share on other sites
-count number of instances of each letter in ''source'' word
-count number of instances of each letter in ''candidate'' word
-for each letter /l/:
-if source.numL < candidate.numL, can''t make the word
-If not yet rejected by the for loop, can make the word.

##### Share on other sites
Hello,
This is not homework no. Yesterday people misunderstud my question and thought I wanted to check the word BECAUSE with BCEAUSE. Where as I want to see how many words can be made out of a string. I have a dictionairty and I check if the word is real but what I want to do is simply make as many words out of ''TheMainWord''

Cheers,
Rob

##### Share on other sites
The solution depends upon the size of your dictionary compared to the number of characters in "TheMainWord". A 10 letter word will form 9,864,000 (3 to 10 letter) combinations.

If your "TheMainWord" is always short, you can build each possible sequence of letters and do a look up in your dictionary and count the matches.

If your word can be long, you may just want to run through the dictionary one word at a time and see if it can be built from the letters given. You can enhance the speed by skipping portions of the dictionary that could not be part of the combinations (i.e. if the word is "Sharp", there is no reason to search the "B"s or any word with more than 5 letters).

##### Share on other sites
The words are going to be random and long so I cannot use that method. I have an idea but its going to be long and bad programming and it will not be memory efficient. But its the only way I can think off. Please try and offer me more soloutions!

##### Share on other sites
Ok, since I could not use all the code I have been given and people have been telling me things that I already know and are not my question (probably due to my vague question)

I am wondering how do I do this.

string MainWord = "Computer";

TextBox.Text = "out";

[button clicked]

IF THE WORD OUT CAN BE MADE OUT OF "COMPUTER" THEN TELL ME OTHERWISE; SAY U CANNOT MAKE THAT WORD.

How do I do that in C#? Any help would be great!

Thanks,
Rob

##### Share on other sites
I would probably start by sorting the letters in each string. Then go through each letter in the second string. Find the position of the letter in the first string. If it''s not there, return false. If it is there remove all the letters in the first string up to the position, and then remove the first letter in the second string and repeat the process until there are no letters in the second string. If you get to no letters in the second string then return true.

Any code?

Thanks,
Rob

##### Share on other sites
Haven''t tested it, but I think it would be something like:
static bool contains(string s1, string s2) {  char [] a1 = s1.ToCharArray();  char [] a2 = s2.ToCharArray();  Array.Sort(a1);  Array.Sort(a2);  s1 = new string(a1);  s2 = new string(a2);  while (s2 != "") {    int position = s1.IndexOf(s2[0]);    if (position == -1) return false;    s1 = s1.Remove(0, position + 1);    s2 = s2.Remove(0, 1);  }  return true;}

##### Share on other sites
What is the point of sorting? Just count the instances of each letter in WordMadeOutOfMainWord and make sure there are the same number in MainWord.

##### Share on other sites
Hello,
How do I go to that function? contains(); does not work. How do I get to it then?

##### Share on other sites
[EDIT; nevermind - foot->mouth]

what do you mean contains() doesn't work? does it not compile? does it crash? did you copy his code and compile it? show us how you used the function.

-me

[edited by - Palidine on March 24, 2004 3:59:16 PM]

##### Share on other sites
How does it? I am trying to access the code from a picturebox click. But how do I goto that function? What code do you want? I don''t have any thats why I am asking you. The other algorithms do not work out and compare single characters.

Thanks,
Rob

##### Share on other sites
quote:
Original post by rob64464
Hello,
How do I go to that function? contains(); does not work. How do I get to it then?

here is where you said contains(); does not work. that implies you tried to use his function and it didn''t work. so how did you try and use his function?

um, basic stuff in case this is where you''re stumbling:

this is how you call one function from another
bool contains(string s1, string s2) {  char [] a1 = s1.ToCharArray();  char [] a2 = s2.ToCharArray();  Array.Sort(a1);  Array.Sort(a2);  s1 = new string(a1);  s2 = new string(a2);  while (s2 != "") {    int position = s1.IndexOf(s2[0]);    if (position == -1) return false;    s1 = s1.Remove(0, position + 1);    s2 = s2.Remove(0, 1);  }  return true;}int main(){    std::string one = "one";    std::string two = "two";    if (contains(one, two))        std::cout << "it''s in there\n";    else        std::cout << "it''s not in there\n";}

-me

##### Share on other sites
quote:
Original post by CodeMunkie
What is the point of sorting? Just count the instances of each letter in WordMadeOutOfMainWord and make sure there are the same number in MainWord.

The sorting allows for short-circuiting when searching for each individual letter in the second word. By always removing from the front part of each string, the intermediate values are easier to follow in a debugger. Also, the algorithm chosen was one that was expressible in a minimal number of control constructs. Basically, it''s code that tries to be easy to write correctly the first time and easy to debug if it does go wrong.

##### Share on other sites
quote:
Original post by rob64464
How does it? I am trying to access the code from a picturebox click. But how do I goto that function?

##### Share on other sites
quote:
Original post by Palidine
this is how you call one function from another

// code snipped //

This is C# not C++.

##### Share on other sites
quote:
Original post by SiCrane
The sorting allows for short-circuiting when searching for each individual letter in the second word. By always removing from the front part of each string, the intermediate values are easier to follow in a debugger. Also, the algorithm chosen was one that was expressible in a minimal number of control constructs. Basically, it''s code that tries to be easy to write correctly the first time and easy to debug if it does go wrong.

Counting the number of occurrances of each letter has the same effect, yes? In which case, I gave a useful answer before the OP started claiming that people were misunderstanding him. Teh Grr.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627636
• Total Posts
2978331

• 10
• 12
• 22
• 13
• 33