我對C很新穎,我想我會嘗試製作一個解析字符串輸入以揭示真實單詞的程序,並讓它工作,但有些單詞會得到意想不到的結果。下面是正常工作的它的一個例子:在C中解讀字符串
輸入字:停止
匹配:選擇採用
比賽:後
比賽:盆
比賽:現貨
比賽:停止 比賽:上衣
我使用的是一個名爲「aspell」的程序獲得的單詞列表,它允許我創建一個完整的文件RDS。奇怪的是,當我輸入「測試」或「足球」這樣的單詞時,響應會帶回包含原本不存在於輸入單詞中的字母的單詞。我在這裏做錯了什麼?以下是我的解讀功能,它完成了大部分工作。另外,我會發布「足球」例如
int unscrambleWord(int fgLetters) {
// integer used for the counter
int i = 0;
// first make sure that the lengths of the word and of the list word is the same
if(strlen(currentLine) == strlen(input)) {
// loop through each letter in the word given
for(i = 0; i < strlen(input); i++) {
// search the line for the current letter, if we find it increment fgLetters
if(strchr(currentLine, input[i]) != NULL)
fgLetters++;
} // end for
// once we have finished looping through the word; evaluate fgLetters
if(fgLetters == strlen(input)) {
// fgLetters will be equal to the length of the word if each letter appears in the word
printf("\tMatch: %s \n", currentLine);
} // end if - evaluate length of fgLetters
}
// return the fgLetters after we have possibly incremented it
return fgLetters;
}
這裏是足球的例子:
Enter Word:football
Match: blastoff
Match: boastful
Match: flatboat
Match: football
Match: lifeboat
Match: softball
出於某種原因,有s在這場比賽中的字符串,但似乎字符數是一樣。
我喜歡你的算法。 – EricSchaefer 2012-03-12 21:05:30
是的,這是一個好主意,我沒有想到! – 2012-03-12 21:07:42