Let's say I have two strings: main_word: 'abcdefa' and check_word: 'abcd'.
I know that the letters from check_word are all in the main_word.
I have to write a function that would return me the rest of the main_word after 'using all the letters to form the check_word. In the above example the function would return the string efa.
Here's my code:
private static string getResidue(string, main_word, string check_word)
{
string result = "";
bool isFound;
foreach (char c in main_word)
{
isFound = false;
for(int i = 0; i < check_word.Length; ++i)
{
if (c == check_word[i])
{
//check_word[i] = 'x'; mark as used (this doesn't work)
isFound = true;
break;
}
}
if (!isFound) result += c;
}
return result;
}
The problem is that this version doesn't support duplicates of letters. I commented the version that would solve my problem, but unfortunately, the c# doesn't allow that line because the property of indexer cannot be assigned to - it is read only. Any ideas how to make this function work as intended?
Aucun commentaire:
Enregistrer un commentaire