C++ 查找其中字母最少的单词数
C++ Find the number of words that have the fewest letters in it
我查找字母最少的单词的代码是这样的:
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;
}
相关文章:
- 在数组中查找总计
- 在二维数组中查找最小值和最大值?
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 查找自动生成键并具有线性内存消耗的小型关联数组
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 在C++中查找(奇怪的)字符串中的单词
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 为什么使用数组元素查找最大数字的程序不起作用?
- 在数组中查找重复模式
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 当您在此单词中搜索单词时调整字符数组的大小?
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 将字节数组 (uint8_t) 转换为单词数组 (uint16_t),反之亦然
- 我该如何找到部分单词匹配/查找c++
- 应用于单词数组的随机数范围
- C++帮助:使用数组和循环在文本中查找单词
- C++ 在单词中查找字谜
- 查找并替换字符数组中的单词
- 搜索数组以查找单词的单独实例
- 从文件中读取行数、单词数、字符数