华为OD机试C卷(100分)-英文输入法(C语言)

题目描述

主管期望你来实现英文输入法单词联想功能。

需求如下:

依据用户输入的单词前缀,从已输入的英文语句中联想出用户想输入的单词,按字典序输出联想到的单词序列,
如果联想不到,请输出用户输入的单词前缀。
注意:

英文单词联想时,区分大小写
缩略形式如”don’t”,判定为两个单词,”don”和”t”
输出的单词序列,不能有重复单词,且只能是英文单词,不能有标点符号

输入描述

输入为两行。

首行输入一段由英文单词word和标点符号组成的语句str;

接下来一行为一个英文单词前缀pre。

0 < word.length() <= 20
0 < str.length <= 10000
0 < pre <= 20

输出描述

输出符合要求的单词序列或单词前缀,存在多个时,单词之间以单个空格分割

用例

输入 I love you
He
输出 He
说明
从用户已输入英文语句”I love you”中提炼出“I”、“love”、“you”三个单词,接下来用户输入“He”,
从已输入信息中无法联想到任何符合要求的单词,因此输出用户输入的单词前缀。

输入
The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don’t know that I love you.
f
输出 front furthest
说明 从用户已输入英文语句”The furthestdistance in the world, Is not between life and death, But when I stand in frontof you, Yet you dont know that I love you.”中提炼出的单词,符合“f”作为前缀的,有“furthest”和“front”,按字典序排序并在单词间添加空格后输出,结果为“front furthest”。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORDS 100
#define MAX_WORD_LENGTH 50

int compare(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

void suggestWords(char sentence[], char prefix[]) {
    char *words[MAX_WORDS];
    int wordCount = 0;

    // Tokenize the sentence into words
    char *token = strtok(sentence, " ,.;");
    while (token != NULL) {
        words[wordCount] = token;
        wordCount++;
        token = strtok(NULL, " ,.;");
    }

    // Sort the words in alphabetical order
    qsort(words, wordCount, sizeof(char *), compare);

    // Find words starting with the given prefix
    printf("Suggested words: ");
    int found = 0;
    for (int i = 0; i < wordCount; i++) {
        if (strncmp(words[i], prefix, strlen(prefix)) == 0) {
            printf("%s ", words[i]);
            found = 1;
        }
    }

    if (!found) {
        printf("%s", prefix);
    }

    printf("\n");
}

int main() {
    char sentence[1000];
    char prefix[MAX_WORD_LENGTH];

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    printf("Enter a word prefix: ");
    scanf("%s", prefix);

    suggestWords(sentence, prefix);

    return 0;
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会起名字呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值