• 华为机试——拼写检查程序


    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 != '') && (*s2 != ''))
        {
            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;
    }
  • 相关阅读:
    Redis的数据结构
    Centos6.5安装Redis3.0备忘记录
    Centos6.5安装MySQL5.6备忘记录
    QueryRunner使用之可变条件的处理
    List集合遍历整理
    js获取select标签选中的值
    linuxC/C++面试问题总结整理
    live555学习(一)通读Makefile编译live555
    入园博客开篇
    springboot 整合 MongoDB 实现登录注册,html 页面获取后台参数的方法
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3194148.html
Copyright © 2020-2023  润新知