華為機試——拼寫檢查程序
系統
1681 0
C_C++_XY_08
.
拼寫檢查程序
請設計一個自動拼寫檢查函數,對輸入單詞的錯誤依據字典進行修正。
1. 輸入為一個單詞和一組字典單詞,每個單詞長度不超過9位;
2. 若字典中沒有與輸入相同的單詞,認為輸入單詞錯誤,需要從字典中選擇一個修正單詞;
3. 修正要求:與輸入單詞長度相同,且單詞中不同字符數最少;
4. 存在多個修正單詞時,取字典中的第一個;
5. 輸出修正后的單詞。
void FixWord(const char *pInputWord, long lWordLen, const char pWordsDic[][MAX_WORD_LEN], long lDicLen, char *pOutputWord);
【輸入】pInputWord: 輸入的單詞
lWordLen: 單詞長度
pWordsDic: 字典單詞數組
lDicLen: 字典單詞個數
【輸出】 pOutputWord: 輸出的單詞,空間已經開辟好,與輸入單詞等長
【注意】不考慮空單詞和找不到長度相同的單詞等異常情況。
輸入:“goad”
字典:“god good wood”
輸出:“good”
?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
|
#include
?
<iostream>
#include
?
<string.h>
using
?
namespace
std;
#define
MAX_WORD_LEN 9
?
//
長度相同,對應位字符不同的個數。
int
?
diffCharCount
(
const
?
char
*s1,
const
?
char
*s2)
{
???
int
count = 0;
???
while
((*s1 !=
'\0'
) && (*s2 !=
'\0'
))
??? {
???????
if
(*s1 != *s2)
??????? {
??????????? count++;
??????? }
??????? s1++;
??????? s2++;
??? }
???
return
count;
}
?
void
?
FixWord
(
const
?
char
*pInputWord,
long
lWordLen,
const
?
char
pWordsDic[][MAX_WORD_LEN],
long
lDicLen,
char
*pOutputWord)
{
???
if
(pInputWord == NULL)
//Invalid input
略。
??? {
???????
return
;
??? }
?
???
int
inputWordLen =
strlen
(pInputWord);
???
//
求每個單詞的長度。
???
int
wordLen[lDicLen];
???
for
(
int
i = 0; i < lDicLen; i++)
??? {
??????? wordLen[i] =
strlen
(pWordsDic[i]);
??? }
?
???
int
minDiffCount = MAX_WORD_LEN;
???
//
比較輸入單詞與單詞表中長度相同單詞。
???
for
(
int
j = 0; j < lDicLen; j++)
??? {
???????
if
(inputWordLen == wordLen[j])
??????? {
???????????
//
長度相同時,比較各個字符是否相同。
???????????
if
(
strcmp
(pInputWord,pWordsDic[j]) == 0)
//
字典中有該單詞。
??????????? {
???????????????
strcpy
(pOutputWord, pInputWord);
??????????? }
???????????
else
??????????? {
???????????????
int
tmp;
??????????????? tmp = diffCharCount(pWordsDic[j], pInputWord);
???????????????
if
(tmp < minDiffCount)
??????????????? {
??????????????????? minDiffCount = tmp;
???????????????????
strcpy
(pOutputWord, pWordsDic[j]);
??????????????? }
??????????? }
??????? }
??? }
}
?
?
int
?
main
() {
?
???
const
?
char
*pInputWord =
"goad"
;
???
const
?
char
pWordsDic[][MAX_WORD_LEN] = {
"god"
,
"good"
,
"wood"
};
???
char
pOutputWord[MAX_WORD_LEN];
?
??? FixWord(pInputWord, 4, pWordsDic, 3, pOutputWord);
?
??? cout << pOutputWord << endl;
?
?
???
return
0;
}
|
華為機試——拼寫檢查程序
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元