making letters out of a word [c#]

Started by
17 comments, last by rob64464 20 years ago
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]
Advertisement
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]
-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.
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
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).
mdp
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!
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
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
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;} 

This topic is closed to new replies.

Advertisement