華為機(jī)試——拼寫檢查程序
系統(tǒng)
1605 0
C_C++_XY_08
.
拼寫檢查程序
請(qǐng)?jiān)O(shè)計(jì)一個(gè)自動(dòng)拼寫檢查函數(shù),對(duì)輸入單詞的錯(cuò)誤依據(jù)字典進(jìn)行修正。
1. 輸入為一個(gè)單詞和一組字典單詞,每個(gè)單詞長度不超過9位;
2. 若字典中沒有與輸入相同的單詞,認(rèn)為輸入單詞錯(cuò)誤,需要從字典中選擇一個(gè)修正單詞;
3. 修正要求:與輸入單詞長度相同,且單詞中不同字符數(shù)最少;
4. 存在多個(gè)修正單詞時(shí),取字典中的第一個(gè);
5. 輸出修正后的單詞。
void FixWord(const char *pInputWord, long lWordLen, const char pWordsDic[][MAX_WORD_LEN], long lDicLen, char *pOutputWord);
【輸入】pInputWord: 輸入的單詞
lWordLen: 單詞長度
pWordsDic: 字典單詞數(shù)組
lDicLen: 字典單詞個(gè)數(shù)
【輸出】 pOutputWord: 輸出的單詞,空間已經(jīng)開辟好,與輸入單詞等長
【注意】不考慮空單詞和找不到長度相同的單詞等異常情況。
輸入:“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
?
//
長度相同,對(duì)應(yīng)位字符不同的個(gè)數(shù)。
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);
???
//
求每個(gè)單詞的長度。
???
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])
??????? {
???????????
//
長度相同時(shí),比較各個(gè)字符是否相同。
???????????
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;
}
|
華為機(jī)試——拼寫檢查程序
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元