华为机试——拼写检查程序
系统
1955 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元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元