- 题目描述:
请设计一个自动拼写检查函数,对输入单词的错误依据字典进行修正。
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”
#include "stdafx.h"
#include <iostream>
using namespace std;
const char input[]="good";
const int MAX_WORD_LEN=9;
const int Dic_len=3;
const int input_len=4;
const char pWordsDic[Dic_len][MAX_WORD_LEN]={"god","god","wod"};
//void FixWord(const char *pInputWord,long lWordLen,const char pWordsDic[][MAX_WORD_LEN],long lDicLen,char *pOutputWord)
//{
//
//}
int _tmain(int argc, _TCHAR* argv[])
{
int *all_len=new int [Dic_len];
for(int i=0;i<Dic_len;++i)
{
all_len[i]=strlen(pWordsDic[i]);
}
int diffrent=0;
int temp=input_len;
int count=0;
for(int i=0;i<Dic_len;++i)
{
diffrent=0;
if(input_len!=all_len[i])
continue;
for(int j=0;j<input_len;++j)
{
if(input[j]!=pWordsDic[i][j])
diffrent++;
if(diffrent<temp)
{
temp=diffrent;
count=i;
}
}
}
cout<<pWordsDic[count];
return 0;
}