Replace occurance of character with all letters in the alphabet
Posted
by
McAvoy
on Stack Overflow
See other posts from Stack Overflow
or by McAvoy
Published on 2011-11-27T17:39:44Z
Indexed on
2011/11/27
17:50 UTC
Read the original article
Hit count: 165
I have created a scrabble game with a computer opponent. If a blank tile is found in the computer's rack during the word generation if needs to be swapped out for every letter in the alphabet. I have my current solution to solve this problem below, but was wondering if there is a better more efficient way to accomplish this task.
if (str.Contains("*"))
{
char c = 'A';
String made = "";
while(c < 'Z')
{
made = str.ReplaceFirst("*", c.ToString());
if (!made.Contains("*"))
{
wordsMade.Add(made);
if (theGame.theTrie.Search(made) == Trie.SearchResults.Found)
{
validWords.Add(made);
}
}
else
{
char ch = 'A';
String made2 = "";
while (ch < 'Z')
{
made2 = made.ReplaceFirst("*", c.ToString());
wordsMade.Add(made2);
if (theGame.theTrie.Search(made2) == Trie.SearchResults.Found)
{
validWords.Add(made2);
}
ch++;
}
}
c++;
}
© Stack Overflow or respective owner