C++ 查找其中字母最少的单词数

C++ Find the number of words that have the fewest letters in it

本文关键字:单词数 查找 C++      更新时间:2023-10-16

我查找字母最少的单词的代码是这样的:

cin.get(a, 100);
p = strtok(a," ");
min = p;
int ok;
while (p) {
    if (strlen(min) > strlen(p)) {
        strcpy(min, p);
    }
    p = strtok(NULL," ");
}

 cout << "The word with the fewest lettes is " << min << endl;

我的问题是我怎么能找到它出现的次数?对不起,如果这是一个愚蠢的问题,我是 c++ 的初学者。

只需添加一个简单的计数器变量,该变量在开始时为 0,当有一个字符较少的单词时更改为 1,如果有一个单词的字符数与现在字符最少的单词相同,则增加。

我认为这样的事情会奏效。

enter code herecin.get(a, 100);
p = strtok(a," ");
min = p;
int ok;
int counter = 0;
while (p) {
    if (strlen(min) > strlen(p)) {
        strcpy(min, p);
        counter = 1;
     }
    else if(strlen(min) == strlen(p)){
        counter++;
    }
    p = strtok(NULL," ");
}

使用std::string的简单快速的解决方案。您可以在此处阅读有关std::string的信息。这是标准库附带的强大类,如果您掌握了它,您的生活将变得更加轻松。这里讨论了std::size_t的优点

#include <iostream>
#include <string>
int main ()
{
    std::string input;
    std::cin >> input;
    std::size_t curPos = 0;
    std::size_t length = input.length();
    std::size_t sw_offset = 0; // sw means 'shortest word'
    std::size_t sw_length = 0;
    std::size_t sw_count = 0;
    while(curPos <= length)
    {
        std::size_t newPos = input.find_first_of(' ', curPos); 
        if(newPos == std::string::npos) // If there is no whitespace it means it's only 1 word
            newPos = length;
        if(newPos != curPos) // If word isn't empty (currentWordLength > 0)
        {
            std::size_t currentWordLength = newPos - curPos;
            if(!sw_length || sw_length > currentWordLength)
            {
                sw_offset = curPos; // Store offset and length instead of copying
                sw_length = currentWordLength;
                sw_count = 1;
            }
            else if(sw_length == currentWordLength &&
                input.substr(sw_offset, sw_length) == input.substr(curPos, currentWordLength))
            {
                ++sw_count;
            }
        }
        curPos = newPos + 1;
    }
    std::cout << "Fewest letter word is " << input.substr(sw_offset, sw_length)
                 << " and it's appeared " << sw_count << " time(s)" << std::endl;
}

与 c 样式字符串相同

#include <iostream>
#include <cstring>
#include <algorithm>
int main ()
{
    char input[256];
    std::cin.get(input, sizeof(input));
    std::size_t curPos = 0;
    std::size_t length = strlen(input);
    std::size_t sw_offset = 0;
    std::size_t sw_length = 0;
    std::size_t sw_count = 0;
    while(curPos <= length)
    {
        std::size_t newPos = std::find(input + curPos, &input[sizeof(input) - 1], ' ') - input;
        std::size_t currentWordLength = newPos - curPos;
        if(currentWordLength > 0)
        {
            if(!sw_length || sw_length > currentWordLength)
            {
                sw_offset = curPos;
                sw_length = currentWordLength;
                sw_count = 1;
            }
            else if(sw_length == currentWordLength &&
                      strncmp(input + sw_offset, input + curPos, currentWordLength) == 0)
            {
                ++sw_count;
            }
        }
        curPos = newPos + 1;
    }
    char result[256];
    strncpy(result, input + sw_offset, sw_length);
    std::cout << "Fewest letter word is " << result
                 << " and it's appeared " << sw_count << " time(s)" << std::endl;
}